CMake:使静态库有效,但动态库不起作用
CMake: Make a static library works but dynamic doesn't
我正在尝试使用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 Linker不同)默认情况下不允许未定义的符号共享库中的参考。
您有两个选择:
您可以指定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)
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 不明白为什么动态编程在这里不起作用
- CMake:使静态库有效,但动态库不起作用
- C 排序字符串的动态阵列 - 排序不起作用
- MEMSET FUCTION在我的C 动态阵列初始化中不起作用
- 使用动态语言的QT应用程序(QTranslator)不起作用
- 动态分配不起作用
- 多维动态数组,为什么不起作用?
- 我计算两个动态数组乘积的函数不起作用
- 在使用包含类的动态实例化后调用 c++ 重载运算符 [] 似乎不起作用
- 对象的 C++ 向量,包含动态分配的内存 - "擦除"不起作用
- C++ 中的动态对象不起作用
- 删除动态 2D 数组不起作用
- 在运行时动态加载库不起作用
- c++中的动态链接不起作用
- 为什么在动态数组上以特定方式调用'delete'不起作用?