当将可执行文件链接到第三方依赖共享库时,c++ CMake未定义引用。

时间:2022-09-18 14:18:34

I read a lot of related topics (like 1, 2, 3) but did not find the answer by myself so here I am.

我读了很多相关的话题(比如1、2、3),但没有找到答案,所以我就在这里。

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".

我有一个构建和可执行的CMake项目,让我们说“x”。我创建了一个名为“a”的共享库。所以,这取决于其他的共享库,称为“b.so”。我想在x中使用a。

Here is my simplified "x" CMakelists.txt:

这是我的简化的“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:

“x”编译输出提取:

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.

“a”和“b”确实可以编译。

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.

问题是,当我想要编译x时,我在链接时出错:未定义的引用“函数名”。我也试着把它和“b”联系起来,但它还是不起作用。

Here "b" also appears but I get the same error...

这里“b”也出现了,但我也有同样的错误…

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!

谢谢!

2 个解决方案

#1


0  

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

所以,看起来你的b。所以找不到ALSA图书馆。如果您使用CMake 3.0(和up),您可以将它添加到您的CMakeLists中。三通过

find_package( ALSA )

and can append ${ALSA_INCLUDE_DIR} to your include path and ${ALSA_LIBRARY} to your linker path.

并且可以将${ALSA_INCLUDE_DIR}添加到您的链接路径和${ALSA_LIBRARY}。

#2


0  

"b" was missing a specific dependant library. I added it via the FIND_PACKAGE CMake directive in the "b" CMakeLists.txt. It solved the problem.

“b”缺失了一个特定的依赖库。我在“b”CMakeLists.txt中通过FIND_PACKAGE CMake指令添加了它。它解决了这个问题。

The above "x" CMakeLists.txt is thus correct.

上面的“x”CMakeLists。三是正确的。

#1


0  

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

所以,看起来你的b。所以找不到ALSA图书馆。如果您使用CMake 3.0(和up),您可以将它添加到您的CMakeLists中。三通过

find_package( ALSA )

and can append ${ALSA_INCLUDE_DIR} to your include path and ${ALSA_LIBRARY} to your linker path.

并且可以将${ALSA_INCLUDE_DIR}添加到您的链接路径和${ALSA_LIBRARY}。

#2


0  

"b" was missing a specific dependant library. I added it via the FIND_PACKAGE CMake directive in the "b" CMakeLists.txt. It solved the problem.

“b”缺失了一个特定的依赖库。我在“b”CMakeLists.txt中通过FIND_PACKAGE CMake指令添加了它。它解决了这个问题。

The above "x" CMakeLists.txt is thus correct.

上面的“x”CMakeLists。三是正确的。