将 Boost 1.61.0 导入 C++ 项目时出错
Error while importing Boost 1.61.0 to C++ project
我尝试导入 Boost 1.61.0(从 SourceForge - Boost 1.61.0 下载为 .7z),但失败了。
控制台:
"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 里面有什么:
在其他主题中,我看到 lib
文件夹。但在这里我没有看到它。我应该在 BOOST_LIBRARYDIR
?
中输入什么
更新 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 说:
If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work.
我会尝试使用 Cygwin。
如果您是 C++ 的新手,我建议您下载由 Stephan T.Lavavej(Microsoft C++ 开发人员)维护的 MinGW 发行版:https://nuwen.net/mingw.html。它与其他工具和库一起包含预构建的 boost 二进制文件。解压它并通过 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
由于性能原因不会更新成功的结果(在 CLion 中可以通过 Cmake toolbar | Cache | red arrows icon
完成)。
一些补充说明:
Boost_USE_STATIC_LIBRARIES
不是要手动设置的,它是由运行find_package(Boost)
设置的,它使用BOOST_ROOT
或BOOST_INCLUDEDIR + BOOST_LIBRARYDIR
,你应该设置那些如果需要的话。您不必使用我链接的 MinGW 发行版来执行此操作,因为它已经在可访问的位置提供了增强包含和库。
- 您可以通过查看 CMake 缓存中的
Boost_*
变量来检查库的路径是否正确。
libs
boost 源中的目录与问题无关,它不包含任何二进制文件
- 您下载的是使用 Visual Studio 工具链而非 MinGW 构建的 boost 二进制文件,因此它们与您的设置不兼容。如果您不想使用我链接的 MinGW 包,您必须找到使用正确的 MinGW 版本构建的 boost 二进制文件或自己构建它。
我尝试导入 Boost 1.61.0(从 SourceForge - Boost 1.61.0 下载为 .7z),但失败了。
控制台:
"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 里面有什么:
在其他主题中,我看到 lib
文件夹。但在这里我没有看到它。我应该在 BOOST_LIBRARYDIR
?
更新 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 说:
If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work.
我会尝试使用 Cygwin。
如果您是 C++ 的新手,我建议您下载由 Stephan T.Lavavej(Microsoft C++ 开发人员)维护的 MinGW 发行版:https://nuwen.net/mingw.html。它与其他工具和库一起包含预构建的 boost 二进制文件。解压它并通过 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
由于性能原因不会更新成功的结果(在 CLion 中可以通过 Cmake toolbar | Cache | red arrows icon
完成)。
一些补充说明:
Boost_USE_STATIC_LIBRARIES
不是要手动设置的,它是由运行find_package(Boost)
设置的,它使用BOOST_ROOT
或BOOST_INCLUDEDIR + BOOST_LIBRARYDIR
,你应该设置那些如果需要的话。您不必使用我链接的 MinGW 发行版来执行此操作,因为它已经在可访问的位置提供了增强包含和库。- 您可以通过查看 CMake 缓存中的
Boost_*
变量来检查库的路径是否正确。 libs
boost 源中的目录与问题无关,它不包含任何二进制文件- 您下载的是使用 Visual Studio 工具链而非 MinGW 构建的 boost 二进制文件,因此它们与您的设置不兼容。如果您不想使用我链接的 MinGW 包,您必须找到使用正确的 MinGW 版本构建的 boost 二进制文件或自己构建它。