2017-03-31 90 views
0

我正在接近這套庫,因爲我需要爲我的大學考試創建一個個人項目,並且我想用網絡功能做一些事情。我已經有了一本關於這個特定庫的書,但現在我無法在我的項目中鏈接這本書(我的書只解釋瞭如何在Linux上與GCC鏈接,但我在Windows 10上使用MinGW將CLion聯合起來)。 當我編譯一個簡單的空項目是這樣的:在CLion中鏈接Boost.Asio時出現Static_initialization_and_distruction錯誤

#include <boost/asio.h> 

int main (int argc, char ** argv) { 

    return 0; 
} 

我得到這個令人印象深刻的錯誤:

CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0': 
    C:/boost_1_63_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()' 
    C:/boost_1_63_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()' 
    C:/boost_1_63_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()' 
    CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv': 
    C:/boost_1_63_0/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()' 
    CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh': 
    C:/boost_1_63_0/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `[email protected]' 
    CMakeFiles\Hello.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE': 
    C:/boost_1_63_0/boost/asio/detail/impl/winsock_init.ipp:56: undefined  reference to `[email protected]' 
    collect2.exe: error: ld returned 1 exit status 
    CMakeFiles\Hello.dir\build.make:96: recipe for target 'Hello.exe' failed 
    CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hello.dir/all' failed 
    mingw32-make.exe[3]: *** [Hello.exe] Error 1 
    mingw32-make.exe[2]: *** [CMakeFiles/Hello.dir/all] Error 2 
    mingw32-make.exe[1]: *** [CMakeFiles/Hello.dir/rule] Error 2 
    CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hello.dir/rule' failed 
    mingw32-make.exe: *** [Hello] Error 2 
    Makefile:117: recipe for target 'Hello' failed 

,這裏是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.6) 
    project(Hello) 


    set(CMAKE_CXX_STANDARD 14) 

    set(SRC_FILES main.cpp) 
    include_directories(C:\\boost_1_63_0) 
    link_directories(C:\\boost_1_63_0) 

    find_package(BOOST 1.63.0 REQUIRED) 

    add_executable(Hello ${SRC_FILES}) 

我怎樣才能解決這個問題? 在此先感謝!

編輯

我更新了我的CMakeLists.txt,因爲它遵循:

cmake_minimum_required(VERSION 3.8.0) 
project(Hello) 


set(CMAKE_CXX_STANDARD 14) 

set(SRC_FILES main.cpp) 

set(BOOST_ROOT C:\\boost_1_63_0) 
find_package(Boost 1.63.0 COMPONENTS system REQUIRED) 
find_library(WS2_32_LIBRARY ws2_32) 

link_directories(C:\\boost_1_63_0) 
link_libraries(${BOOST_LIBRARIES}) 

add_executable(Hello ${SRC_FILES}) 
target_link_libraries(Hello ${WS2_32_LIBRARY} ${BOOST_LIBRARIES}) 

現在我沒有得到任何錯誤,反正當我告訴我的程序:

#include <boost/asio.hpp> 

提升變紅,靜態分析器告訴我「無法找到提升」。 現在的問題在哪裏? 謝謝!

回答

0

您應該Boost.SystemWinsock鏈接,所以添加這樣的事情你的CMakeLists.txt:

find_package(Boost COMPONENTS system REQUIRED) 
find_library(WS2_32_LIBRARY ws2_32) 
target_link_libraries(Hello ${WS2_32_LIBRARY} ${Boost_SYSTEM_LIBRARY}) 
+0

@Baffo拉斯塔IUUC,現在你的程序編譯和運行,但你的IDE不承認包括什麼呢?我相信你應該配置IDE並且告訴它頭文件駐留的地方。如果您需要幫助,請發佈其他問題並正確標記。 –

+0

不,我的程序甚至沒有編譯,它說構建「無法找到提升」。無論如何,CMake調試告訴我它可以找到升壓1.63.0和升壓::系統... –

+0

@Baffo rasta也許你沒有建立升壓爲明,對嗎? –