IT story

g ++로 다중 스레드 코드 컴파일

hot-time 2020. 9. 16. 20:58
반응형

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;
}

나는 여전히 그것을 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.

컴파일 명령 :

$ g++ main.cpp -o main.out -pthread -std=c++11

달리는:

$ ./main.out 
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

그리고 지금은 막혔습니다. 인터넷상의 모든 관련 스레드에서 -pthread이미 가지고있는 동안 추가 하는 것이 좋습니다 .

내가 뭘 잘못하고 있죠?

추신 : 완전히 새로운 우분투 13.10 설치입니다. g++패키지 설치되었고 사소한 것 chromium

PPS :

$ ldd ./a.out 
linux-vdso.so.1 => (0x00007fff29fc1000) 
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb85397d000) 
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb853767000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb85339e000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb85309a000) 
/lib64/ld-linux-x86-64.so.2 (0x00007fb853c96000)

PPPS : clang++(v3.2)를 사용하면 잘 컴파일되고 실행됩니다.

PPPPS : 얘들 아, 리눅스에서 GCC에서 std :: thread를 사용하는 올바른 링크 옵션무엇입니까?

PPPPPS :

$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin                    install
libc6-dev:amd64                 install
libclang-common-dev             install
linux-libc-dev:amd64                install

대답은 SO C ++ 채팅 의 친절한 회원이 제공했습니다 .

It looks like this behaviour is caused by a bug in gcc.

The workaround provided in the last comment of that bug discussion does work and solves the issue:

-Wl,--no-as-needed

Adding -lpthread fixed the identical problem for me:

 g++ -std=c++11 foo.cpp -lpthread -o foo

I have slightly more advanced version (4.8.4 instead of 4.8.1), and I tested all three answers above. In fact:

-pthread alone works:

g++ -std=c++11 -o main -pthread main.cpp

-Wl,--no-as-needed alone does not work.

-lpthread alone does not work.

-Wl,--no-as-needed and -lpthread together work:

g++ -std=c++11 -o main -Wl,--no-as-needed main.cpp -lpthread

My version is "g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4".


answer already was made for qtcreator:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11

for console g++: here

g++ -c main.cpp -pthread -std=c++11         // generate target object file
g++ main.o -o main.out -pthread -std=c++11  // link to target binary

참고URL : https://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g

반응형