![](/img/trans.png)
[英]Linking to a static library that uses boost libraries causes link errors in cmake
[英]Remove boost dependency when linking to a library that uses boost with cmake
我正在嘗試創建一個在其中使用boost文件系統的庫,但我不想將該依賴項添加到使用該庫的可執行文件中。 我正在使用CMAKE,因為它必須跨平台。
在cmake文件中,我添加了預處理器定義'BOOST_ALL_NO_LIB'來不自動鏈接,並將cmake設置為使用靜態庫。 所有這些都會編譯並創建庫。 cmake代碼如下所示。
set(Boost_USE_STATIC_LIBS ON)
add_definitions(-DBOOST_ALL_NO_LIB)
find_package(Boost REQUIRED COMPONENTS system filesystem)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIR})
target_link_libraries(Utility
${Boost_LIBRARIES})
但是,當我在庫中鏈接時,會出現以下錯誤。
Error 1 error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ...
如果我將boost文件系統和boost系統庫添加到庫的庫管理器的其他依賴項中,則可執行文件中的錯誤就會消失。 我可以用CMAKE做到這一點嗎?
看起來像使用Visual Studio可以實現,但通常不是這樣:
Boost查找模塊中有一個名為Boost_USE_STATIC_LIBS
的選項。 將其設置為“ ON”。 我的Boost CMake罐頭看起來像:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.