簡體   English   中英

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 鏈接器不同)默認情況下不允許共享庫中的未定義符號引用。

您有兩個選擇:

您可以使用target_link_libraries指定xxx-core依賴於其鏈接的所有庫

或者,您可以使用鏈接選項-undefined=dynamic_lookup覆蓋鏈接器的默認行為,指示共享庫中未定義的引用將由加載程序在運行時解析。 在您的 CMakeLists.txt 中,在定義xxx-core目標后使用set_target_properties

ADD_LIBRARY(xxx-core SHARED ${CORE_SRC})
SET_TARGET_PROPERTIES(xxx-core LINK_FLAGS Wl,-undefined=dynamic_lookup)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM