相关疑难解决方法(0)

在linux下的GCC中使用std :: thread的正确链接选项是什么?

嗨,我正在尝试使用std::threadG ++.这是我的测试代码

#include <thread>
#include <iostream>

int main(int, char **){
    std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
    tt.join();
}
Run Code Online (Sandbox Code Playgroud)

它编译,但当我尝试运行它时,结果是:

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted 
Aborted
Run Code Online (Sandbox Code Playgroud)

我的编译器版本:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

我的测试代码出了什么问题?

更新:我使用以下命令行来编译和运行我的代码.

$ g++ -std=c++0x test.cpp
$ ./a.out
Run Code Online (Sandbox Code Playgroud)

我试过 …

c++ multithreading g++ c++11

87
推荐指数
3
解决办法
5万
查看次数

用g ++编译多线程代码

我有最简单的代码:

#include <iostream>
#include <thread>

void worker()
{
    std::cout << "another thread";
}

int main()
{
    std::thread t(worker);
    std::cout << "main thread" << std::endl;
    t.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然我仍然无法编译它g++来运行.

更多细节:

$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

编译命令:

$ g++ main.cpp -o main.out -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)

运行:

$ …
Run Code Online (Sandbox Code Playgroud)

c++ linux ubuntu gcc g++

85
推荐指数
4
解决办法
8万
查看次数

如何检查在给定进程的运行时加载了哪些共享库?

有没有办法检查哪些库是正在运行的进程?

更具体地说,如果程序使用dlopen加载某些共享库,则readelf或ldd不会显示它.是否有可能从正在运行的进程中获取该信息?如果有,怎么样?

c c++ linux shared-libraries

47
推荐指数
5
解决办法
5万
查看次数

当main.c不使用pthreads时,为什么必须在main.c编译中显式链接到pthread?

在Linux中,我有一个使用pthreads的共享库,而没有使用的是main.c.

libpthread.so显示在我的共享库的ldd中,这是正确的.

$ ldd libmapreduce.so.1.0 
    linux-gate.so.1 =>  (0x0067d000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x0058c000)
    [...]
Run Code Online (Sandbox Code Playgroud)

但是当我编译并链接我的main.c,它不使用pthreads到我的共享库时,我看到:

$ icc -Wall -o main main.c -lmapreduce
    /opt/intel/Compiler/11.1/046/lib/ia32/libiomp5.so: undefined reference to `pthread_atfork'
Run Code Online (Sandbox Code Playgroud)

将-lpthread添加到我的编译命令,即

$ icc -Wall -o main main.c -lmapreduce -lpthread
Run Code Online (Sandbox Code Playgroud)

解析未定义的引用.

当我的main.c不使用它并且我的共享库已经链接了libpthread时,为什么我需要显式链接到libpthread?

c shared-libraries ld icc

2
推荐指数
1
解决办法
3063
查看次数

标签 统计

c++ ×3

c ×2

g++ ×2

linux ×2

shared-libraries ×2

c++11 ×1

gcc ×1

icc ×1

ld ×1

multithreading ×1

ubuntu ×1