0

I am approaching to this set of libraries cause I need to create a personal project for my university exam and I'd like to do something with networking features. I already got a book about this specific library but now i can't link this in my project (my book explains only how to link it on linux with GCC, but I'm uning CLion with MinGW on Windows 10). When I compile a simple empty project like this:

#include <boost/asio.h>

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

   return 0;
}

I get this impressive error:

   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 `WSAStartup@8'
   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 `WSACleanup@0'
   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

Here is my 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})

How can I solve this? Thanks in advance!

EDIT

I updated my CMakeLists.txt as it follows:

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

And now I don't get any error, anyway when I tell my program:

#include <boost/asio.hpp>

the boost gets red and the static analyzer tells me "Cannot find boost". Where's the problem now? Thanks!

Baffo rasta
  • 320
  • 1
  • 4
  • 17

1 Answers1

0

You should link with Boost.System and Winsock, so add something like this to your 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})
Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • @Baffo rasta IUUC, now your program compiles and runs, but your IDE does not recognize the include? I believe you should configure the IDE and tell it where the header files reside. If you need an assistance with that, please post another question and tag it properly. – Igor R. Apr 07 '17 at 07:48
  • No, my program doesn't even compile, it says on build "Could not find Boost". Anyway CMake debug tells me it could both find Boost 1.63.0 and boost::system... – Baffo rasta Apr 07 '17 at 08:10
  • @Baffo rasta Perhaps you have not built Boost for mingw, have you? – Igor R. Apr 07 '17 at 13:48
  • l'm sure I did! – Baffo rasta Apr 07 '17 at 14:10