c++ - 使用clion为什么cmake引入boost不成功

查看:704
本文介绍了c++ - 使用clion为什么cmake引入boost不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

报错信息

"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++两天。请帮帮我 :)

解决方案

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()

这篇关于c++ - 使用clion为什么cmake引入boost不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆