C ++ CMake将可执行文件链接到第三方依赖共享库时未定义的引用 [英] C++ CMake undefined reference when linking an executable to a third party dependant shared library
问题描述
我阅读了很多相关主题(例如 1 , 2 , 3 ),但是没有找到我自己的答案,所以这里。
我有一个CMake项目生成和可执行,让说x。我创建了一个名为a.so的共享库,它依赖于其他名为b.so的共享库。我想在x中使用a。
这里是我简化的xCMakelists.txt:
SET(ROOT_DIR $ {CMAKE_CURRENT_SOURCE_DIR})
LINK_DIRECTORIES($ {ROOT_DIR} / lib / a / bin /)#包含liba.so
INCLUDE_DIRECTORIES($ {ROOT_DIR} / lib / a / include /)#包含a标头
ADD_EXECUTABLE(x $ {SOURCE})
TARGET_LINK_LIBRARIES (xa)
x编译输出extract:
$ b b
链接CXX可执行文件../bin/x
/ usr / bin / cmake -E cmake_link_script CMakeFiles / x.dir / link.txt --verbose = 1
/ usr / lib64 / ccache / c ++ -std = c ++ 0x CMakeFiles / x.dir / src / main /Main.cpp.o
... -L /.../ lib / a / bin -rdynamic -la -Wl,-rpath,/ ... / lib / a / bin
a和b做编译。
问题是当我想编译x我在链接时得到错误:未定义的引用'function name'。
我试图链接到b,但它仍然不工作。
这里b相同的错误...
链接CXX可执行文件../bin/x
/ usr / bin / cmake - E cmake_link_script CMakeFiles / x.dir / link.txt --verbose = 1
/ usr / lib64 / ccache / c ++ -std = c ++ 0x CMakeFiles / x.dir / src / main
/ Main .cpp.o -o ../bin/x -L /.../lib/b/bin -L/.../lib/a/bin
-rdynamic -lb -la -Wl, - rpath,/ ... / lib / b / bin:/.../ lib / a / bin
这里是错误输出:
$ make
[20%]目标的自动moc x
链接CXX可执行文件../bin/x
/.../lib/b/bin/b.so:未定义对`snd_pcm_sw_params_set_start_threshold'的引用
/.../lib/b/bin/ b.so:未定义对`snd_seq_delete_simple_port'的引用
/.../lib/b/bin/b.so:未定义对`snd_pcm_info_set_device'的引用
/.../lib/b/bin/ b.so:未定义引用`snd_pcm_sw_params'
/.../lib/b/bin/b.so:未定义对`snd_pcm_sw_params_set_silence_threshold'的引用
/.../lib/b/bin/ b.so:未定义引用`snd_pcm_hw_params_any'
/.../lib/b/bin/b.so:未定义对`snd_seq_drain_output'的引用
/.../lib/b/bin/ b.so:未定义引用`snd_ctl_pcm_next_device'
...
collect2:错误:ld返回1退出状态
CMakeFiles / x.dir / build。 make:163:目标'../bin/x'失败的食谱
make [2]:*** [../bin/x]错误1
CMakeFiles / Makefile2:60:recipe for目标'CMakeFiles / x.dir / all'失败
make [1]:*** [CMakeFiles / x.dir / all]错误2
Makefile:76:目标'all'失败的食谱
make:*** [all]错误2
我看不到什么问题,我现在很困惑,因为我尝试了很多事情来让事情工作...但没有工作。
有什么想法吗?
谢谢!
我问过上面的注释中的错误输出,因为它可以帮助搜索未定义的符号/参考。
因此,看来你的b.so找不到ALSA库。如果您使用 CMake 3.0 (和更高版本),您可以通过
添加到
CMakeLists.txt
b $ b ,并可将 $ {ALSA_INCLUDE_DIR}
附加到include路径和 $ {ALSA_LIBRARY}
到您的链接器路径。
I read a lot of related topics (like 1, 2, 3) but did not find the answer by myself so here I am.
I have a CMake project that builds and executable, let say "x". I created a shared library named "a.so" that depends on other shared library called "b.so". I want to use "a" in "x".
Here is my simplified "x" CMakelists.txt:
SET(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
LINK_DIRECTORIES(${ROOT_DIR}/lib/a/bin/) # contains liba.so
INCLUDE_DIRECTORIES(${ROOT_DIR}/lib/a/include/) # contains "a" headers
ADD_EXECUTABLE(x ${SOURCE})
TARGET_LINK_LIBRARIES(x a)
"x" compilation output extract:
Linking CXX executable ../bin/x
/usr/bin/cmake -E cmake_link_script CMakeFiles/x.dir/link.txt --verbose=1
/usr/lib64/ccache/c++ -std=c++0x CMakeFiles/x.dir/src/main /Main.cpp.o
... -L/.../lib/a/bin -rdynamic -la -Wl,-rpath,/.../lib/a/bin
"a" and "b" do compile.
The problem is when I want to compile x I get errors when linking: undefined reference to 'function name'. I tried to link against "b" too but it's still not working.
Here "b" also appears but I get the same error...
Linking CXX executable ../bin/x
/usr/bin/cmake -E cmake_link_script CMakeFiles/x.dir/link.txt --verbose=1
/usr/lib64/ccache/c++ -std=c++0x CMakeFiles/x.dir/src/main
/Main.cpp.o -o ../bin/x -L/.../lib/b/bin -L/.../lib/a/bin
-rdynamic -lb -la -Wl,-rpath,/.../lib/b/bin:/.../lib/a/bin
Here is the error output:
$ make
[ 20%] Automatic moc for target x
Linking CXX executable ../bin/x
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params_set_start_threshold'
/.../lib/b/bin/b.so: undefined reference to `snd_seq_delete_simple_port'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_info_set_device'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_sw_params_set_silence_threshold'
/.../lib/b/bin/b.so: undefined reference to `snd_pcm_hw_params_any'
/.../lib/b/bin/b.so: undefined reference to `snd_seq_drain_output'
/.../lib/b/bin/b.so: undefined reference to `snd_ctl_pcm_next_device'
...
collect2: error: ld returned 1 exit status
CMakeFiles/x.dir/build.make:163: recipe for target '../bin/x' failed
make[2]: *** [../bin/x] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/x.dir/all' failed
make[1]: *** [CMakeFiles/x.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
I don't see what's wrong and I'm now confused as I tried many things to get the thing working... but nothing worked.
Any idea?
Thanks!
I asked you for the error output in the comments above, because it can help to search the undefined symbols/references.
So, it seems that your b.so cannot find the ALSA library. If you use CMake 3.0 (and up) you can add it to your CMakeLists.txt
via
find_package( ALSA )
and can append ${ALSA_INCLUDE_DIR}
to your include path and ${ALSA_LIBRARY}
to your linker path.
这篇关于C ++ CMake将可执行文件链接到第三方依赖共享库时未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!