我正在将第三方库与我的可执行文件链接起来。我已经成功链接了许多其他库。我无法理解我做错了什么。
这是导致问题的部分
# Build libssh 0.7.3
ExternalProject_Add(libssh2
URL ${PROJECT_SOURCE_DIR}/packages/libssh.tar.gz
PREFIX ${CMAKE_BINARY_DIR}/external/libssh2
CONFIGURE_COMMAND ""
UPDATE_COMMAND ""
BUILD_COMMAND mkdir -p <SOURCE_DIR>/build && cd <SOURCE_DIR>/build &&
cmake -DWITH_STATIC_LIB=1 -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=Debug .. && make
INSTALL_COMMAND cd <SOURCE_DIR>/build && make install PREFIX=<INSTALL_DIR>
)
ExternalProject_Get_Property(libssh2 install_dir)
add_library(libssh STATIC IMPORTED)
set_property(TARGET libssh PROPERTY IMPORTED_LOCATION
${install_dir}/lib/libssh.a)
add_dependencies(libssh libssh2)
include_directories(${install_dir}/include)
这是另一个正确链接的第三方
ExternalProject_Add(libevent2
URL ${PROJECT_SOURCE_DIR}/packages/libevent-2.0.21-stable.tar.gz
PREFIX ${CMAKE_BINARY_DIR}/external/libevent2
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
UPDATE_COMMAND ""
)
ExternalProject_Get_Property(libevent2 install_dir)
add_library(libevent STATIC IMPORTED)
set_property(TARGET libevent PROPERTY IMPORTED_LOCATION
${install_dir}/lib/libevent.a)
add_dependencies(libevent libevent2)
include_directories(${install_dir}/include)
我看到的错误
Linking C executable ../../bin/nixia
CMakeFiles/nixia.dir/main_config.c.o: In function `main_config_dump':
/home/nikhil/nixia/src/main/main_config.c:88: undefined reference to `ssh_new'
/home/nikhil/nixia/src/main/main_config.c:91: undefined reference to `ssh_options_set'
collect2: ld returned 1 exit status
正确创建了库
[nikhil@nikhil lib]$ ls
cmake libssh.a libssh.so libssh.so.4 libssh.so.4.4.1 libssh_threads.a libssh_threads.so libssh_threads.so.4 libssh_threads.so.4.4.1 pkgconfig
[nikhil@nikhil lib]$ pwd
/home/nikhil/nixia/build/external/libssh2/lib
该库包含函数定义
[nikhil@nikhil lib]$ ar x libssh.a | nm * | grep ssh_new
nm: Warning: 'cmake' is not an ordinary file
U ssh_new
0000000000000000 T ssh_new
00000000000351b0 T ssh_new
00000000000351b0 T ssh_new
00000000000351b0 T ssh_new
答案 0 :(得分:1)
我建议您使用CMake verbose模式进行编译,以检查用于编译它的完整命令。
为此,您必须使用以下命令进行编译:
make VERBOSE=1
通过这种方式,您可以检查哪个链接的库标识了问题。
未定义引用错误的可能原因: