链接器无法在库中找到符号

时间:2017-05-31 02:40:44

标签: c++ c cmake linker

我使用CMake构建了一个C库。这是库的CMakeLists文件:

cmake_minimum_required(VERSION 3.7)
project(sshserial)

set(CMAKE_C_STANDARD 99)

include_directories(BEFORE SYSTEM ${CMAKE_SOURCE_DIR}/common/include)
link_directories(${CMAKE_SOURCE_DIR}/common/lib)

set(SOURCE_FILES sshserial.c sshserial.h)
add_library(sshserial ${SOURCE_FILES})

TARGET_LINK_LIBRARIES(sshserial libssh.a)

我在C ++程序中使用这个库。这是该程序的CMakeLists文件:

cmake_minimum_required(VERSION 3.7)
project(sshtest)

set(CMAKE_CXX_STANDARD 11)

include_directories(BEFORE SYSTEM ${CMAKE_SOURCE_DIR}/common/include)
link_directories(${CMAKE_SOURCE_DIR}/common/lib)

set(SOURCE_FILES main.cpp)
add_executable(sshtest ${SOURCE_FILES})

TARGET_LINK_LIBRARIES(sshtest libsshserial.a)

我已将库复制到正确的文件夹中。当我在库上运行nm时,我得到了这个:

$ nm libsshserial.a 

libsshserial.a(sshserial.c.o):
                 U ___error
                 U ___stack_chk_fail
                 U ___stack_chk_guard
                 U ___stderrp
                 U ___stdinp
0000000000000330 T _create_session
                 U _exit
                 U _fgets
                 U _fprintf
                 U _free
                 U _getpass
0000000000000490 T _read_from_session
                 U _ssh_channel_close
                 U _ssh_channel_free
                 U _ssh_channel_new
                 U _ssh_channel_open_session
                 U _ssh_channel_read
                 U _ssh_channel_request_exec
                 U _ssh_channel_send_eof
                 U _ssh_connect
                 U _ssh_disconnect
                 U _ssh_free
                 U _ssh_get_error
                 U _ssh_get_hexa
                 U _ssh_get_pubkey_hash
                 U _ssh_is_server_known
                 U _ssh_new
                 U _ssh_options_set
                 U _ssh_print_hexa
                 U _ssh_userauth_password
                 U _ssh_write_knownhost
                 U _strerror
                 U _strncasecmp
0000000000000000 T _verify_knownhost
                 U _write

当我尝试构建程序时,我收到此链接器错误:

Undefined symbols for architecture x86_64:
  "create_session()", referenced from:
      _main in main.cpp.o
  "read_from_session(ssh_session_struct*)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [sshtest] Error 1
make[2]: *** [CMakeFiles/sshtest.dir/all] Error 2
make[1]: *** [CMakeFiles/sshtest.dir/rule] Error 2
make: *** [sshtest] Error 2

我已经被困在这里一段时间了,我确信我错过了一些微不足道的事情。我做错了什么?

P.S。我在Mac上

$ uname -a
Darwin XXXXXX 15.6.0 Darwin Kernel Version 15.6.0: Tue Apr 11 16:00:51 PDT 2017; root:xnu-3248.60.11.5.3~1/RELEASE_X86_64 x86_64

1 个答案:

答案 0 :(得分:2)

问题是C ++使用name mangling。要禁止您需要使用extern "C"声明C函数,但仅在由C ++编译器编译时。您可以使用编译C ++时定义的__cplusplus宏来检查这一点,但在编译C时则不行。

在声明函数原型的头文件中,执行类似

的操作
#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

// All your function prototype declarations here

#ifdef __cplusplus
}  // End of extern "C"
#endif