Search code examples
c++cmakeclionasio

How include ASIO C++ in CLion?


I used different options, but all of them didn't work:

1) CMake:

cmake_minimum_required(VERSION 3.14)
project(Client)

set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

FetchContent_Declare(
        asio
        GIT_REPOSITORY https://github.com/chriskohlhoff/asio
        GIT_TAG asio-1-14-0
        UPDATE_DISCONNECTED 1
)

FetchContent_Populate(asio)
include_directories(${asio_SOURCE_DIR}/asio/include/)

add_executable(${PROJECT_NAME} main.cpp)

main.cpp:

#define  ASIO_STANDALONE
#include <iostream>
#include "asio.hpp"

int main(){std::cout<<"Hello, World!"<<std::endl;return 0;}

Error:

CMakeFiles\Client.dir/objects.a(main.cpp.obj): In function `ZN4asio6detail17winsock_init_base7startupERNS1_4dataEhh':
C:/Users/Danila/Documents/Programming/C++/Client/cmake-build-debug/_deps/asio-src/asio/include/asio/detail/impl/winsock_init.ipp:38: undefined reference to `_imp__WSAStartup@8'
CMakeFiles\Client.dir/objects.a(main.cpp.obj): In function `ZN4asio6detail17winsock_init_base7cleanupERNS1_4dataE':
C:/Users/Danila/Documents/Programming/C++/Client/cmake-build-debug/_deps/asio-src/asio/include/asio/detail/impl/winsock_init.ipp:55: undefined reference to `_imp__WSACleanup@0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Client.exe] Error 1

2) Download ASIO in project.

CMake:

cmake_minimum_required(VERSION 3.14)
project(Client)

set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

add_executable(${PROJECT_NAME} main.cpp)

main.cpp

#define  ASIO_STANDALONE
#include <iostream>
#include "asio-master/asio/include/asio.hpp"

int main(){std::cout<<"Hello, World!"<<std::endl;return 0;}

Error:

In file included from C:\Users\Danila\Documents\Programming\C++\Client\asio-master/asio/include/asio.hpp:18:0,
                     from C:\Users\Danila\Documents\Programming\C++\Client\main.cpp:3:
    C:\Users\Danila\Documents\Programming\C++\Client\asio-master/asio/include/asio/associated_allocator.hpp:18:34: fatal error: asio/detail/config.hpp: No such file or directory
    compilation terminated.
    CMakeFiles\Client.dir\build.make:61: recipe for target 'CMakeFiles/Client.dir/main.cpp.obj' failed
    mingw32-make.exe[3]: *** [CMakeFiles/Client.dir/main.cpp.obj] Error 1

What I do wrong?

Also, I tried to install and include asio through boost, but it doesn't work too.


Solution

  • The first method is working. But in windows for mingw need to add link_libraries(ws2_32 wsock32) after include_directories(${asio_SOURCE_DIR}/asio/include/). In linux need add:SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DASIO_STANDALONE -pthread").