1
我剛剛在C++ 2a中爲coroutine
寫了一個測試代碼。如何與clang進行靜態鏈接libC++
我建立了代碼鏗鏘5.0:
clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++
的代碼工作正常。
現在我想靜態鏈接libC++。這樣我就可以在其他PC上運行a.out,但是隻能找到-static-libstdc++
。我不能使用-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.
什麼建議嗎?
測試代碼:
#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