project(learn)
cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_STANDARD 17)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
message("Current OS is Linux")
include_directories("/mnt/e/c++/boost_1_72_0")
link_directories("/mnt/e/c++/boost_1_72_0/stage/lib")
link_libraries(pthread boost_thread boost_fiber boost_context)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
message("Current OS is Windows")
include_directories("E:/c++/boost_1_72_0")
link_directories("E:/c++/boost_1_72_0/stage/lib")
endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
add_executable(learn_asio learn_asio.cpp)
learn_asio.cpp:
#include <boost/asio.hpp>
#include <boost/fiber/all.hpp>
#include <boost/thread.hpp>
#include <iostream>
using boost::asio::async_write;
using boost::asio::buffer;
using boost::asio::io_context;
using boost::asio::use_future;
using boost::asio::ip::make_address;
using boost::asio::ip::tcp;
using boost::fibers::async;
using boost::fibers::fiber;
using boost::system::error_code;
int main(){
io_context ioc;
tcp::socket socket(ioc);
tcp::endpoint ep(make_address("192.168.1.20"), 80);
auto ret_f = socket.async_connect(ep, boost::asio::use_future);
boost::thread_group t;
t.create_thread([&ioc]() {
ioc.run();
std::cout << "jfiejf" << std::endl;
});
ret_f.wait_for(std::chrono::seconds(3));
t.join_all();
return 0;
}
我的图书馆文件夹:通过上面的代码,我可以成功地构建代码。但我讨厌代码:
link_libraries(pthread boost_thread boost_fiber boost_context)
在linux平台上。为什么在Windows平台上不需要它?我记得Linux也可以自动链接库。我该如何实现?