构建库时的 Clang 和未定义符号

2021-12-26 00:00:00 clang cmake c++

我正在开发一个 C++ 框架,当我在 OSX 上使用 Clang 编译它时会出现一些问题.

I'm working on a C++ framework, and there's a few issues when I compile it on OSX with Clang.

首先,我正在使用一些其他库,例如 openssl,并且 clang 抱怨在我构建库时某些符号没有得到解决.它们不应该是:这些库将与最终的二进制文件链接,它不应该发生在中介上.

First of, I'm using some other libraries, such as openssl, and clang complains that some symbols aren't solved when I build the library. They shouldn't be: these libraries will be linked with the final binary, it shouldn't happen on an intermediary.

然后,还有一些方法和变量应该在客户端"二进制文件中实现……使用 GCC,没有问题,但 Clang 也抱怨这些符号在编译过程中无法解决.

Then, there's also a few methods and variables that are supposed to be implemented in the "client" binary... with GCC, no problems, but Clang also complains that these symbols can't be solved during compilation.

怎么会?我该怎么办?

这是我的 CMakeLists.txt,以防万一:

Here's my CMakeLists.txt in case that can be useful:

cmake_minimum_required(VERSION 2.8)

project(crails_project)

set(CMAKE_CXX_FLAGS "-std=c++0x -Wall -Wno-deprecated-declarations -pedantic -DASYNC_SERVER -DSERVER_DEBUG -DUSE_MONGODB_SESSION_STORE")

find_package(cppnetlib REQUIRED)

include_directories(include /usr/local/include ${CPPNETLIB_INCLUDE_DIRS} .)

file(GLOB crails_core
     src/*.cpp)

file(GLOB crails_sql
     src/sql/*.cpp)

file(GLOB crails_mongodb
     src/mongodb/*.cpp)

add_library(crails-core    SHARED ${crails_core})
add_library(crails-sql     SHARED ${crails_sql})
add_library(crails-mongodb SHARED ${crails_mongodb})

这是崩溃的命令:

/usr/bin/c++  -std=c++0x -Wall -Wno-deprecated-declarations -pedantic -DASYNC_SERVER -DSERVER_DEBUG -DUSE_MONGODB_SESSION_STORE -dynamiclib -Wl,-headerpad_max_install_names   -o libcrails-core.dylib -install_name /Users/michael/Personal/crails/build/libcrails-core.dylib CMakeFiles/crails-core.dir/src/assets.cpp.o CMakeFiles/crails-core.dir/src/cgi2params.cpp.o CMakeFiles/crails-core.dir/src/cipher.cpp.o [...]

这是我得到的两种错误:

And here are the two kinds of error I get:

体系结构 x86_64 的未定义符号:

Undefined symbols for architecture x86_64:

  "_BIO_ctrl", referenced from:
      Cipher::encode_base64(unsigned char*, unsigned int) const in cipher.cpp.o

第二个:

  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for boost::detail::thread_data_base", referenced from:
      boost::detail::thread_data_base::thread_data_base() in server.cpp.o

推荐答案

解决了!Clang 需要接收选项 -undefined dynamic_lookup 以在编译库时忽略丢失的符号.

Solved it ! Clang needs to receive the option -undefined dynamic_lookup to ignore missing symbols when compiling a library.

将此添加到 CMakeFile.txt 以产生预期的效果:

Add this to the CMakeFile.txt to produce the expected effect:

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
endif()

相关文章