首页  >  问答  >  正文

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

巴扎黑巴扎黑2764 天前1646

全部回复(4)我来回复

  • 迷茫

    迷茫2017-04-17 14:53:03

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

    回复
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:53:03

    boost库的组件基本都需要链接

    target_link_libraries(untitled1, boost_system)

    回复
    0
  • 黄舟

    黄舟2017-04-17 14:53:03

    undefined reference to boost::system::generic_category()`
    这是说你没有链接boost_system库

    undefined reference to _imp__WSACleanup@0'`
    是说你没有链接Ws2_32.lib库

    回复
    0
  • 高洛峰

    高洛峰2017-04-17 14:53:03

    @ds2016 评论长度超过限制,这是加上DEBUG后的输出

    output

    "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 untitled1 -- -j 4
    -- start running cmake...
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:946 ] _boost_TEST_VERSIONS = 1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:948 ] Boost_USE_MULTITHREADED = TRUE
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:950 ] Boost_USE_STATIC_LIBS = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:952 ] Boost_USE_STATIC_RUNTIME = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:954 ] Boost_ADDITIONAL_VERSIONS = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:956 ] Boost_NO_SYSTEM_PATHS = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1024 ] Declared as CMake or Environmental Variables:
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1026 ]   BOOST_ROOT = C:\www\cpp\boost_1_62_0
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1028 ]   BOOST_INCLUDEDIR = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1030 ]   BOOST_LIBRARYDIR = 
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1032 ] _boost_TEST_VERSIONS = 1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1125 ] location of version.hpp: C:/www/cpp/boost_1_62_0/boost/version.hpp
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1149 ] version.hpp reveals boost 1.62.0
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1235 ] guessed _boost_COMPILER = -mgw53
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1245 ] _boost_MULTITHREADED = -mt
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1288 ] _boost_RELEASE_ABI_TAG = -
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1290 ] _boost_DEBUG_ABI_TAG = -d
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1344 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = C:\www\cpp\boost_1_62_0/lib;C:\www\cpp\boost_1_62_0/stage/lib;C:/www/cpp/boost_1_62_0/lib;C:/www/cpp/boost_1_62_0/../lib;C:/www/cpp/boost_1_62_0/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib_boost_LIBRARY_SEARCH_DIRS_DEBUG   = C:\www\cpp\boost_1_62_0/lib;C:\www\cpp\boost_1_62_0/stage/lib;C:/www/cpp/boost_1_62_0/lib;C:/www/cpp/boost_1_62_0/../lib;C:/www/cpp/boost_1_62_0/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1483 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-mgw53-mt-1_62;boost_system-mgw53-mt;boost_system-mt-1_62;boost_system-mt;boost_system
    CMake Warning at C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:743 (message):
      Imported targets not available for Boost version 106200
    Call Stack (most recent call first):
      C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:842 (_Boost_COMPONENT_DEPENDENCIES)
      C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1395 (_Boost_MISSING_DEPENDENCIES)
      CMakeLists.txt:9 (find_package)
    
    
    CMake Warning at C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:743 (message):
      Imported targets not available for Boost version 106200
    Call Stack (most recent call first):
      C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:842 (_Boost_COMPONENT_DEPENDENCIES)
      C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1395 (_Boost_MISSING_DEPENDENCIES)
      CMakeLists.txt:9 (find_package)
    
    
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1525 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-mgw53-mt-d-1_62;boost_system-mgw53-mt-d;boost_system-mt-d-1_62;boost_system-mt-d;boost_system-mt;boost_system
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1483 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-mgw53-mt-1_62;boost_filesystem-mgw53-mt;boost_filesystem-mt-1_62;boost_filesystem-mt;boost_filesystem
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1525 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-mgw53-mt-d-1_62;boost_filesystem-mgw53-mt-d;boost_filesystem-mt-d-1_62;boost_filesystem-mt-d;boost_filesystem-mt;boost_filesystem
    CMake Error at C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1753 (message):
      Unable to find the requested Boost libraries.
    
      Boost version: 1.62.0
    
      Boost include path: C:/www/cpp/boost_1_62_0
    
      Could not find the following 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.
    Call Stack (most recent call first):
      CMakeLists.txt:9 (find_package)
    
    
    -- [ C:/Program Files (x86)/JetBrains/CLion 2016.2.3/bin/cmake/share/cmake-3.6/Modules/FindBoost.cmake:1595 ] Boost_FOUND = 1
    -- Configuring incomplete, errors occurred!
    See also "C:/Users/shellus/.CLion2016.2/system/cmake/generated/untitled1-f8bd5d82/f8bd5d82/Debug/CMakeFiles/CMakeOutput.log".
    Makefile:175: recipe for target 'cmake_check_build_system' failed
    mingw32-make.exe: *** [cmake_check_build_system] Error 1
    

    CMakeList.txt

    cmake_minimum_required(VERSION 3.6)
    project(untitled1)
    message(STATUS "start running cmake...")
    
    set(BOOST_ROOT "C:\www\cpp\boost_1_62_0")
    set(Boost_DEBUG ON)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    find_package(Boost COMPONENTS system filesystem REQUIRED)
    if(Boost_FOUND)
    
        set(SOURCE_FILES main.cpp)
        add_executable(untitled1 ${SOURCE_FILES})
        target_link_libraries(untitled1 ${Boost_LIBRARIES})
        include_directories(${Boost_INCLUDE_DIRS})
    endif()

    回复
    0
  • 取消回复