c++ - CMake:使静态库有效,但动态无效
问题描述
我正在尝试使用 CMake 制作一个共享库,但如果我静态制作它,它会运行良好,但动态不会:
CMAKE_MINIMUM_REQUIRED(VERSION 3.6)
PROJECT(TestDemo)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_COMPILER clang++)
# Headers
INCLUDE_DIRECTORIES(src)
INCLUDE_DIRECTORIES(src/xxx)
INCLUDE_DIRECTORIES(3rdparty/zstd)
INCLUDE_DIRECTORIES(/usr/local/include)
# CORE LIB
FILE(GLOB CORE_SRC src/xxx/*.cpp
src/xxx/io/*.cpp
src/xxx/util/*.cpp
src/xxx/thread/*.cpp)
LIST(REMOVE_ITEM CORE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/xxx/io/BzipStream.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xxx/io/GzipStream.cpp)
# This works
ADD_LIBRARY(xxx-core STATIC ${CORE_SRC})
# This doesn't
#ADD_LIBRARY(xxx-core SHARED ${CORE_SRC})
错误信息是:
[100%] Linking CXX shared library libxxxdb-core.dylib
Undefined symbols for architecture x86_64:
"_mpool_get_global", referenced from:
xxx::mpoolxx<xxx::alloc_to_mpool_bridge<xxx::mpoolxx<long>, 1>::MemBlock>::get_vtab() in trb_cxx.cpp.o
"_sfixed_mpool_destroy", referenced from:
xxx::fixed_mpool_wrapper<24>::~fixed_mpool_wrapper() in trb_cxx.cpp.o
"_sfixed_mpool_init", referenced from:
xxx::fixed_mpool_wrapper<24>::fixed_mpool_wrapper() in trb_cxx.cpp.o
"_trb_destroy", referenced from:
xxx::trbstrmap_imp<int, unsigned char, &(xxx_trb_compare_less_tag), xxx::mpoolxx<long>, 0, 16>::~trbstrmap_imp() in trb_cxx.cpp.o
xxx::trbtab<int const, std::__1::pair<int const, int>, 0, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::~trbtab() in trb_cxx.cpp.o
"_trb_erase", referenced from:
xxx::trbtab<int const, std::__1::pair<int const, int>, 0, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::erase(int const&) in trb_cxx.cpp.o
xxx::trbtab<int const, std::__1::pair<int const, int>, 0, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::erase(xxx::trb_iterator<std::__1::pair<int const, int>, 16, 0>) in trb_cxx.cpp.o
"_trb_iter_first", referenced from:
xxx::trbtab<int const, std::__1::pair<int const, int>, 0, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::begin() const in trb_cxx.cpp.o
"_trb_iter_next", referenced from:
xxx::trb_iterator<std::__1::pair<int const, int>, 16, 0>::operator++() in trb_cxx.cpp.o
"_trb_probe", referenced from:
xxx::trbtab<int const, std::__1::pair<int const, int>, 0, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::insert(std::__1::pair<int const, int> const&) in trb_cxx.cpp.o
xxx::trbmap<int, int, &(xxx_trb_compare_less_tag), xxx::fixed_mpoolxx<long>, 0, 16>::operator[](int const&) in trb_cxx.cpp.o
"_trb_probe_node", referenced from:
xxx::trbstrmap_imp<int, unsigned char, &(xxx_trb_compare_less_tag), xxx::mpoolxx<long>, 0, 16>::probe_raw(char const*, unsigned long, char const*) in trb_cxx.cpp.o
"_trb_vtab_init", referenced from:
xxx::trbxx_vtab_init_by_cxx_type(trb_vtab*, field_type_t, int (*)(trb_vtab const*, trb_tree const*, void const*, void const*), int (*)(trb_vtab const*, trb_tree const*, void const*, void const*)) in trb_cxx.cpp.o
xxx::trbxx_vtab_init(trb_vtab*, field_type_t, int (*)(trb_vtab const*, trb_tree const*, void const*, void const*)) in trb_cxx.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)
解决方案
在创建静态库时不会发生链接,因为静态库不是由链接器生成的:它只是使用归档器创建的目标文件的归档ar
。因此,当您创建静态库时,不会有任何未定义的引用或其他链接错误,就像您创建目标文件.tar
或.zip
归档时一样。见静态库,
共享库与程序一样,由链接器生成,Mac OS/Darwin 链接器(与 GNU/Linux 链接器不同)默认情况下不允许在共享库中引用未定义的符号。
你有两个选择:
您可以指定xxx-core
依赖于其链接的所有库,使用target_link_libraries
或者,您可以使用链接选项覆盖链接器的默认行为
-undefined=dynamic_lookup
,指示共享库中未定义的引用将由加载器在运行时解析。在您的 CMakeLists.txt 中,set_target_properties
在定义xxx-core
目标后使用:
ADD_LIBRARY(xxx-core SHARED ${CORE_SRC})
SET_TARGET_PROPERTIES(xxx-core LINK_FLAGS Wl,-undefined=dynamic_lookup)
推荐阅读
- amazon-web-services - 使用 cognito 登录而不是证书来验证和订阅 aws IoT MQTT 主题?
- jsf - p:datePicker dateSelect 空日期事件
- ios - 快速滚动时跳过 UIScrollView 委托函数
- sql - 在 UNION 之后过滤重复项
- python - 如何使用 gitpython 克隆不与 git 关联的 git 存储库?
- hibernate - 休眠和 MySQL
- python - 使用递归绘制嵌套三角形
- c++ - 在 Ubuntu 上使用 libc++ 的 clang 编译器出现问题
- ios - 如何使用 SwiftUI 重置子视图状态变量?
- javascript - 对象值仅在数组的第一个元素中增加