我遇到了不确定如何解决的问题。我相信这是GCC和/或libstdc++中的问题。

我正在使用GCC 4.8.2-19ubuntu1,libstdc++ 3.4.19(我相信?How do you find what version of libstdc++ library is installed on your linux machine?)和Ubuntu 1.55运行Ubuntu 14.04 LTS。

这是代码:

// http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/tutorial.html
// with a slight modification to ensure we're testing with threads too
// g++ -g -O0 --std=c++11 staticlinktest.cpp -lboost_log_setup -lboost_log -lboost_system -lboost_filesystem -lboost_thread -lpthread

#define BOOST_ALL_DYN_LINK 1

#include <boost/log/trivial.hpp>

#include <thread>
#include <atomic>
#include <vector>

int main(int, char*[])
{
    BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
    BOOST_LOG_TRIVIAL(info) << "An informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
    BOOST_LOG_TRIVIAL(error) << "An error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

    std::atomic<bool> exiting(false);
    std::vector<std::thread> threads;
    for ( int i = 0; i < 8; ++i ) {
        threads.push_back(std::thread([&exiting](){
            while (!exiting)
                BOOST_LOG_TRIVIAL(trace) << "thread " << std::this_thread::get_id() << " trace";
        }));
    }

    usleep(1000000);
    exiting = true;
    std::for_each(threads.begin(), threads.end(), [](std::thread& t){
        t.join();
    });

    return 0;
}

问题:
使用顶部的命令行,我将使用动态链接进行构建。一切似乎都很好。我看到带有线程ID和跟踪信息的看似有效的输出。

但是,在我的项目中,我需要能够使用静态链接。因此,我将“-static”开关添加到g++命令中,并注释掉BOOST_ALL_DYN_LINK的#define。它建立得很好。但是,当我执行该程序时,它会一直运行直到创建第一个线程,然后出现段错误。回溯似乎总是相同的:
#0  0x0000000000000000 in ?? ()
#1  0x0000000000402805 in __gthread_equal (__t1=140737354118912, __t2=0) at /usr/include/x86_64-linux-gnu/c++/4.8/bits/gthr-default.h:680
#2  0x0000000000404116 in std::operator== (__x=..., __y=...) at /usr/include/c++/4.8/thread:84
#3  0x0000000000404c03 in std::operator<< <char, std::char_traits<char> > (__out=..., __id=...) at /usr/include/c++/4.8/thread:234
#4  0x000000000040467e in boost::log::v2s_mt_posix::operator<< <char, std::char_traits<char>, std::allocator<char>, std::thread::id> (strm=...,
    value=...) at /usr/include/boost/log/utility/formatting_ostream.hpp:710
#5  0x0000000000402939 in __lambda0::operator() (__closure=0x7bb5e0) at staticlinktest.cpp:27
#6  0x0000000000403ea8 in std::_Bind_simple<main(int, char**)::__lambda0()>::_M_invoke<>(std::_Index_tuple<>) (this=0x7bb5e0)
    at /usr/include/c++/4.8/functional:1732
#7  0x0000000000403dff in std::_Bind_simple<main(int, char**)::__lambda0()>::operator()(void) (this=0x7bb5e0)
    at /usr/include/c++/4.8/functional:1720
#8  0x0000000000403d98 in std::thread::_Impl<std::_Bind_simple<main(int, char**)::__lambda0()> >::_M_run(void) (this=0x7bb5c8)
    at /usr/include/c++/4.8/thread:115
#9  0x000000000047ce60 in execute_native_thread_routine ()
#10 0x000000000042a962 in start_thread (arg=0x7ffff7ffb700) at pthread_create.c:312
#11 0x00000000004e5ba9 in clone ()

在我看来,它似乎是在尝试仅在静态链接时才调用空函数指针。有什么想法吗?难道我做错了什么?

最佳答案

libpthread静态链接到您的应用程序是really bad idea

尽管如此,这是如何做到的。

我首先修复了编译错误(我怀疑您没有向我们展示您实际上正在编译的代码,或者boost严重污染了 namespace ),然后删除了不相关的boost内容,这只会给问题增加噪音。这是代码:

#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>

int main(int, char*[])
{
    std::atomic<bool> exiting(false);

    std::vector<std::thread> threads;
    for ( int i = 0; i < 8; ++i ) {
        threads.push_back(std::thread([&exiting](){
            while (!exiting)
                std::cout << "thread " << std::this_thread::get_id() << " trace\n";
        }));
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(1));

    exiting = true;

    for(auto& t : threads){
        t.join();
    };

    return 0;
}

如果我动态链接,它运行良好,但静态链接时崩溃:
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)

根据this e-mail,如果您使用线程和静态链接,则必须正确配置libstdc++。这是使它与静态链接一起使用的不可思议的标志:
g++ -std=c++11 -pedantic -pthread threads.cpp -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive

如前所述,将libpthread静态链接到您的应用程序会带来麻烦。

10-08 05:37
查看更多