gpt4 book ai didi

c++ - 如何在 CLion 中包含 ASIO C++?

转载 作者:行者123 更新时间:2023-11-30 04:43:17 26 4
gpt4 key购买 nike

我使用了不同的选项,但它们都不起作用:

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)

主要.cpp:

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

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

错误:

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) 在工程中下载ASIO

CMake:

cmake_minimum_required(VERSION 3.14)
project(Client)

set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

add_executable(${PROJECT_NAME} main.cpp)

主要.cpp

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

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

错误:

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

我做错了什么?

此外,我尝试通过 boost 安装并包含 asio,但它也不起作用。

最佳答案

第一种方法有效。但是在windows中mingw需要在include_directories(${asio_SOURCE_DIR}/asio/include/)之后添加link_libraries(ws2_32 wsock32)。在 linux 中需要添加:SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DASIO_STANDALONE -pthread")

关于c++ - 如何在 CLion 中包含 ASIO C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com