1
私はちょうどcoroutine
のテストコードをC++ 2aに書いています。clangと静的にリンクする方法libC++
clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++
コードが正常に動作します:
私はと打ち鳴らす5.0でコードをビルドします。
libC++を静的にリンクしたいので、他のPCでa.outを実行できるように、私はグーグルで-static-libstdc++
しか見つけません。
libstdc++
はcoroutine
をサポートしていないため、-static-libstdc++
は使用できません。
clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts
-static-libstdc++
testcoroutine.cpp:26:10: fatal error: 'experimental/coroutine' file not found
#include <experimental/coroutine>
^~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
どれ提案:私は-static-のlibstdC++を使用している場合
?
テストコード:
#define ASIO_STANDALONE
#define ASIO_HAS_STD_CHRONO
#ifdef _WIN32
#pragma warning (disable:4819)
#pragma warning (disable:4503)
#pragma warning (disable:4996)
#pragma warning (disable:4100) // unref parameters
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#define _CRT_NONSTDC_NO_DEPRECATE
#define WIN32_LEAN_AND_MEAN
#endif
#ifdef _WIN32
#include <SDKDDKVer.h>
#endif
#include <stdio.h>
#include <cstdio>
#include <thread>
#include <algorithm>
#include <future>
#include <chrono>
#include <experimental/coroutine>
#include <asio.hpp>
// clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++
#ifndef _WIN32
template <typename... Args>
struct std::experimental::coroutine_traits<std::future<void>, Args...> {
struct promise_type {
std::promise<void> p;
auto get_return_object() { return p.get_future(); }
std::experimental::suspend_never initial_suspend() { return {}; }
std::experimental::suspend_never final_suspend() { return {}; }
void set_exception(std::exception_ptr e) { p.set_exception(std::move(e)); }
void return_void() { p.set_value(); }
void unhandled_exception() { std::terminate(); }
};
};
#endif
template <typename R, typename P>
auto async_await(asio::steady_timer &t, std::chrono::duration<R, P> d) {
struct Awaiter {
asio::steady_timer &t;
std::chrono::duration<R, P> d;
asio::error_code ec;
bool await_ready() { return d.count() == 0; }
void await_resume() {
if (ec)
throw ec;
}
void await_suspend(std::experimental::coroutine_handle<> coro) {
t.expires_from_now(d);
t.async_wait([this, coro](auto ec) mutable {
this->ec = ec;
coro.resume();
});
}
};
return Awaiter{ t, d };
}
std::future<void> sleepy(asio::io_service &io) {
asio::steady_timer timer(io);
co_await async_await(timer, std::chrono::milliseconds(100));
puts("tick1");
co_await async_await(timer, std::chrono::milliseconds(100));
puts("tick2");
co_await async_await(timer, std::chrono::milliseconds(100));
puts("tick3");
}
int main()
{
asio::io_service io;
sleepy(io);
io.run();
return 0;
}
私は「-static」しようとしたが、リンクエラーのトンを得ました。 – alpha
エラーメッセージを含め、質問にその情報を追加します。 – rustyx