报错信息
"C:\Program Files (x86)\JetBrains\CLion 2016.2.3\bin\cmake\bin\cmake.exe" --build C:\Users\shellus\.CLion2016.2\system\cmake\generated\untitled1-f8bd5d82\f8bd5d82\Debug --target all -- -j 4 -- Boost version: 1.62.0 -- Configuring done -- Generating done -- Build files have been written to: C:/Users/shellus/.CLion2016.2/system/cmake/generated/untitled1-f8bd5d82/f8bd5d82/Debug Scanning dependencies of target untitled1 [ 50%] Building CXX object CMakeFiles/untitled1.dir/main.cpp.obj [100%] Linking CXX executable untitled1.exe CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0': C:/www/cpp/boost_1_62_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()' C:/www/cpp/boost_1_62_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()' C:/www/cpp/boost_1_62_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()' CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv': C:/www/cpp/boost_1_62_0/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()' CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh': C:/www/cpp/boost_1_62_0/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `_imp__WSAStartup@8' CMakeFiles\untitled1.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE': C:/www/cpp/boost_1_62_0/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[2]: *** [untitled1.exe] Error 1 CMakeFiles\untitled1.dir\build.make:96: recipe for target 'untitled1.exe' failed mingw32-make.exe[1]: *** [CMakeFiles/untitled1.dir/all] Error 2 CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/untitled1.dir/all' failed mingw32-make.exe: *** [all] Error 2 Makefile:82: recipe for target 'all' failed
CMakeList.txt
cmake_minimum_required(VERSION 3.6) project(untitled1) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") include_directories("C:\\www\\cpp\\boost_1_62_0") set(BOOST_ROOT "C:\\www\\cpp\\boost_1_62_0") set(BOOSTROOT "C:\\www\\cpp\\boost_1_62_0") find_package(Boost 1.62.0) if(NOT Boost_FOUND) message(FATAL_ERROR "Could not find boost!") endif() set(SOURCE_FILES main.cpp) add_executable(untitled1 ${SOURCE_FILES})
main.cpp
#include <iostream> #include "boost/asio.hpp" int main() { std::cout << "Hello, World!" << std::endl; return 0; }
我是做php开发的。初学C++两天。请帮帮我 :)
三叔2016-11-10 09:29:59
find_package(Boost 1.62.0)应该改成find_package(Boost REQUIRED COMPONENTS system)
再加上target_link_libraries(untitled1 ${Boost_LIBRARIES}) 让cmake去搞定Boost。
另外,再加上链接Ws2_32.lib库。
if(WIN32) target_link_libraries(untitled wsock32 ws2_32) endif()
三叔2016-11-10 09:29:20
undefined reference to boost::system::generic_category()`
这是说你没有链接boost_system库
undefined reference to _imp__WSACleanup@0'`
是说你没有链接Ws2_32.lib库