如何使用clang libc ++进行静态链接

时间:2017-10-16 07:58:43

标签: c++ clang coroutine libc++

我刚刚在c ++ 2a中为coroutine编写了一个测试代码。

我用clang 5.0构建代码:

clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++

代码工作正常。

现在我想静态链接libc ++。所以我可以在其他PC上运行a.out,我用Google搜索但只找到-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;
}

2 个答案:

答案 0 :(得分:1)

GNU libstdc ++和LLVM libc ++是标准C ++库的两种不同实现。

显然你的libstdc ++还不支持Coroutines TS,所以你必须坚持使用libc ++。

要静态链接您的应用,只需使用-static

clang++ testcoroutine.cpp -std=c++2a -I../asio_alone -fcoroutines-ts -stdlib=libc++ -static

答案 1 :(得分:1)

我遇到了类似的问题。添加this.setState({ data: Object.values(collection) }) 标志后,我(也)遇到了大量“缺少符号”链接器错误。就我而言,缺少的符号都属于libpthread和libc ++ abi库。尝试在命令行(-static)中显式添加这些库依赖项也无济于事-这可能是由于系统中clang使用的默认链接程序处理依赖项的顺序所致。切换到lld链接器后,该问题似乎已解决。总之,我在clang命令中添加了以下4个标志:-lc++abi -pthread

相关问题