2016-08-03 59 views
0

我試圖導入Boost 1.61.0(從SourceForge - Boost 1.61.0下載爲.7z),但失敗。將Boost 1.61.0導入到C++項目時出錯

控制檯:

"D:\Program Files (x86)\JetBrains\CLion 2016.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marczak\.CLion2016.2\system\cmake\generated\WsServer-e351c9f9\e351c9f9\Debug --target WsServer -- -j 4 
[ 50%] Linking CXX executable WsServer.exe 
CMakeFiles\WsServer.dir\build.make:96: recipe for target 'WsServer.exe' failed 
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/WsServer.dir/all' failed 
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0': 
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()' 
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()' 
C:/Users/Marczak/boost_1_61_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()' 
collect2.exe: error: ld returned 1 exit status 
mingw32-make.exe[3]: *** [WsServer.exe] Error 1 
mingw32-make.exe[2]: *** [CMakeFiles/WsServer.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles/WsServer.dir/rule] Error 2 
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/WsServer.dir/rule' failed 
mingw32-make.exe: *** [WsServer] Error 2 
Makefile:117: recipe for target 'WsServer' failed 

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.5) 
project(WsServer) 

set(BOOST_ROOT "C:/Users/Marczak/boost_1_61_0") 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") 

set(SOURCE_FILES src/main.cpp) 

find_package(Boost) 
include_directories(${Boost_INCLUDE_DIRS}) 
add_executable(WsServer ${SOURCE_FILES}) 

如果我做find_package(Boost 1.61.0 COMPONENTS system filesystem REQUIRED)我得到:

Error: Unable to find the requested Boost libraries. 
Boost version: 1.61.0 
Boost include path: C:/Users/Marczak/boost_1_61_0 
Could not find the following static Boost libraries: 
     boost_system   boost_filesystem 
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost. 

我試圖設置Boost_USE_STATIC_LIBRARIES,但是失敗了。我使用CLion 2016.2。

更新:我也試過舊版本。同樣的錯誤。什麼是.7z壓縮內部:

.7z file

在其他議題我看到lib文件夾。但在這裏我沒有看到它。我應該輸入BOOST_LIBRARYDIR

UPDATE 2:https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/安裝的二進制文件。我注意到有新的文件夾:lib64-msvc-14.0。它包含許多.dll和.lib文件,例如boost_atomic-vc140-mt-1_61.dll

Boost.org說:

如果您打算使用工具從Windows命令提示符下,你在正確的地方。如果你打算從Cygwin bash shell構建,你實際上是在一個POSIX平臺上運行,並且應該按照說明開始使用Unix變體。其他命令shell,如MinGW的MSYS,不支持 - 它們可能會或可能不會工作

我會嘗試使用Cygwin。

+0

你用這裏使用的任何編譯器編譯boost嗎?聽起來就像是剛剛在'C:/ Users/Marczak/boost_1_61_0'上提取了源代碼' – drescherjm

+0

@drescherjm號。我是C++新手。如何編譯它? –

+0

也許爲mingw增加一個二進制下載對你會更好。有了這個說法,我不確定什麼編譯器/工具包'CLion'在windows下使用。它是mingw/gcc? – drescherjm

回答

1

如果您是C++的新手,我建議您下載由Stephan T. Lavavej(Microsoft C++開發人員)維護的MinGW發行版:https://nuwen.net/mingw.html。它在其他工具和庫中包含預先構建的增強二進制文件。解壓縮並通過Settings | Build, Execution, Deployment | Toolchains指定路徑。

之後,你應該可以用下面的CMakeLists.txt來編譯程序:

cmake_minimum_required(VERSION 3.5) 
project(WsServer) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") 

set(SOURCE_FILES src/main.cpp) 

find_package(Boost REQUIRED COMPONENTS filesystem) 
include_directories(${Boost_INCLUDE_DIRS}) 
add_executable(WsServer ${SOURCE_FILES}) 
target_link_libraries(WsServer ${Boost_LIBRARIES}) 

不要忘記砸CMake的緩存爲find_packages不更新由於性能原因,成功的結果(在它的克利翁可以通過Cmake toolbar | Cache | red arrows icon完成)。

一些補充說明:

  • Boost_USE_STATIC_LIBRARIES並不意味着手動設置,它是由運行find_package(Boost),它採用BOOST_ROOTBOOST_INCLUDEDIR + BOOST_LIBRARYDIR設置,如果需要的話,你應該設置這些。您無需使用我已鏈接的MinGW發行版,因爲它已在可訪問位置提供了增強包含和庫。
  • 您可以通過查看CMake緩存中的Boost_*變量來檢查庫的路徑是否正確。
  • libs升壓源目錄裏面是毫無關係的問題,它不conitain
  • 你已經下載的Visual Studio工具鏈,而不是MinGW的內置升壓二進制任何二進制,所以它們與您的設置不兼容。如果您不想使用我已鏈接的MinGW軟件包,則必須找到使用正確的MinGW版本構建的增強二進制文件,或者自己構建它。
+0

工作!非常感謝你。 –

相關問題