簡體   English   中英

CMake 找不到 Boost_LIBRARIES 變量

[英]CMake can't find Boost_LIBRARIES variable

最終編輯:已解決。 伙計們,問題是由於“WinSock32”而發生的。 我已經添加

target_link_libraries(modernC__ -lws2_32)

並且代碼已經構建。

我已經在 Ubuntu 中使用 CLion 上的 Boost 庫大約 1 年了。 但本周我也決定將它安裝在 Windows 操作系統上。 所以我在 GitHub 上下載了 Boost 庫,並首先使用 Find.Boost 然后“Find.Boost”安裝了 Boost 庫。

在 CLion 的 CMake 設置部分中編寫必要的命令后,我注意到變量 $ {Boost_LIBRARIES} 為空。

當我不使用消息()function時,項目的“CMake”部分沒有報錯,但是在“build”之后出現錯誤“undefined reference”。 以下是我在 CLion 上編寫的 CMake 命令以及收到的錯誤。

cmake_minimum_required(VERSION 3.16)
project(modernC__)

set(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.66.0)

message(${Boost_INCLUDE_DIR})
message(${Boost_FOUND})
message(${Boost_LIBRARY_DIRS})

IF (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIR})
add_executable(modernC__

        main.cpp

        #concurrencyExampleOne.cpp

        #adapterExampleOne.cpp
        )
target_link_libraries(modernC__ ${Boost_LIBRARIES})
    message(${Boost_LIBRARIES})
endif()

我的 output 是:

"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Berke\CLionProjects\modernC++
C:/boost/include/boost-1_66
TRUE
C:/boost/lib
CMake Error at CMakeLists.txt:22 (message):
  message called with incorrect number of arguments


-- Configuring incomplete, errors occurred!

在這種情況下,顯然 Cmake 目前無法找到應該鏈接到我的代碼的.lib 文件。 我的問題是; 我怎樣才能將.lib 文件永久地放到這個變量中,或者有沒有其他方法可以做到這一點?

如果我不使用 "message(${Boost_LIBRARIES}" 那么編譯器會給我這個錯誤;

[ 50%] Linking CXX executable modernC__.exe
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:676: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:679: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:709: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:721: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv':
C:/boost/include/boost-1_66/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `_imp__WSAStartup@8'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `_imp__WSACleanup@0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\modernC__.dir\build.make:86: modernC__.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/modernC__.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/modernC__.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: modernC__] Error 2

我認為,您應該明確指定 boost 庫(作為組件)。 您應該將庫添加到目標。

像這樣:

find_package(Boost COMPONENTS filesystem system locale context REQUIRED)

...

target_link_libraries(${PROJECT_NAME}
  ...
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
  ${Boost_LOCALE_LIBRARY}
  ${Boost_CONTEXT_LIBRARY}
  ...
  )





暫無
暫無

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

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