【问题标题】:how to static link with clang libc++如何与clang libc ++进行静态链接
【发布时间】:2017-10-16 07:58:42
【问题描述】:

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

我使用 clang 5.0 构建代码:

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

代码运行良好。

现在我想静态链接 libc++.so 以便我可以在其他 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;
}

【问题讨论】:

    标签: c++ clang coroutine libc++


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-10
        • 2012-10-22
        • 1970-01-01
        • 2012-01-19
        • 1970-01-01
        相关资源
        最近更新 更多