使用 cmake 构建项目(使用 GStreamer 1.0,因此依赖于 Glib 2.0)并链接 glib 库时,我遇到了奇怪的链接器错误。 Glib 使用 macports 安装,libglib-2.0.0.dylib 位于/opt/local/lib/中。 FindGLIB 成功找到其头文件(编译工作),并且 ${GLIB_LIBRARIES} 提供了库的正确路径。
错误信息是
[100%] Linking CXX executable ../bin/presenter
Undefined symbols for architecture x86_64:
"_g_object_set", referenced from:
...
"_g_type_check_instance_cast", referenced from:
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果我从列表中完全删除 glib,它也会提示缺少对 g_print 和 g_printerr 的引用。是否有可能链接已正确完成,但由于某种原因在库内缺少函数?
如何修复它?
最佳答案
终于发现我的错误了。当使用Webkit项目的FindGLIB时,它默认只搜索主glib库。除了被发现之外,还必须传递它的组件:
find_package(GLIB COMPONENTS gobject REQUIRED)
会找到glib本身并将其保存在${GLIB_LIBRARIES}
中,也会找到gobject并将其保存在${GLIB_GOBJECT_LIBRARIES}
中,以便它们可以在target_link_libraries中使用()
提醒:请务必阅读文件标题中的注释 - 它们通常包含有用的信息...
# Optionally, the COMPONENTS keyword can be passed to find_package()
# and Glib components can be looked for. Currently, the following
# components can be used, and they define the following variables if
# found:
#
# gio: GLIB_GIO_LIBRARIES
# gobject: GLIB_GOBJECT_LIBRARIES
# gmodule: GLIB_GMODULE_LIBRARIES
# gthread: GLIB_GTHREAD_LIBRARIES
关于macos - MacPorts 中的 glib 的 g_object_set 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281019/