2012-10-15 4 views
6

작은 스레딩 예제 (Linux)를 제대로 컴파일하기 위해 scons를 얻을 수 없습니다. 다음C++ 스콘으로 std :: thread 예제 컴파일

[email protected]:~/projects/c++_threads$ scons 
scons: Reading SConscript files ... 
scons: done reading SConscript files. 
scons: Building targets ... 
g++ -o build/main.o -c -std=c++11 -pthread -Wall -g src/main.cpp 
g++ -o build/c++threads build/main.o 
scons: done building targets. 

내가 ./build/c++threads을 실행할 경우이 오류가 발생합니다 :

내가 SCons는 실행하면,이 수행

내가이 함께 명령 줄에서 컴파일하는 경우
terminate called after throwing an instance of 'std::system_error' 
    what(): Operation not permitted 
Aborted 

:

g++ -std=c++11 -pthread -Wall -g src/main.cpp 

a.out로 컴파일, 내가 실행하는 경우 a.out 그것을 RU ns 프로그램 (스레드에 대한 출력 등).

여기 내 SConstruct 파일입니다 :

# Tell SCons to create our build files in the 'build' directory 
VariantDir('build', 'src', duplicate=0) 

# Set our source files 
source_files = Glob('build/*.cpp', 'build/*.h') 

# Set our required libraries 
libraries = [] 
library_paths = '' 

env = Environment() 

# Set our g++ compiler flags 
env.Append(CPPFLAGS=['-std=c++11', '-pthread', '-Wall', '-g']) 

# Tell SCons the program to build 
env.Program('build/c++threads', source_files, LIBS = libraries, LIBPATH = library_paths) 

여기에 CPP 파일입니다 :

#include <iostream> 
#include <thread> 
#include <vector> 

//This function will be called from a thread 

void func(int tid) { 
    std::cout << "Launched by thread " << tid << std::endl; 
} 

int main() { 
    std::vector<std::thread> th; 

    int nr_threads = 10; 

    //Launch a group of threads 
    for (int i = 0; i < nr_threads; ++i) { 
     th.push_back(std::thread(func,i)); 
    } 

    //Join the threads with the main thread 
    for(auto &t : th){ 
     t.join(); 
    } 

    return 0; 
} 

누구든지 내가 잘못 어떤 생각을 ???

감사합니다.

건배 의견에 대한

@Joachim에

자렛

+3

링커 플래그에도'-pthread'를 추가하지 않아도됩니까? –

+1

@JoachimPileborg 링크 및 컴파일이 두 단계로 수행되면 예. – inf

답변

5

감사 및 @bamboon. pthread를 링커 (scons 라이브러리)에 추가하면 플래그가 작동합니다.

새로운 SCons는 파일

는 지금 : 다시

# Tell SCons to create our build files in the 'build' directory 
VariantDir('build', 'src', duplicate=0) 

# Set our source files 
source_files = Glob('build/*.cpp', 'build/*.h') 

# Set our required libraries 
libraries = ['pthread'] 
library_paths = '' 

env = Environment() 

# Set our g++ compiler flags 
env.Append(CPPFLAGS=['-std=c++11', '-pthread', '-Wall', '-g']) 

# Tell SCons the program to build 
env.Program('build/c++threads', source_files, LIBS = libraries, LIBPATH = library_paths) 

감사합니다!

+0

참고 : 문제가 해결되었음을 보여주기 위해 자신의 대답을 수락 할 수도 있습니다. – inf

+0

분명히, 나 자신의 대답을 받아 들일 수 있기 전에 나는 2 일을 기다려야한다 : \ – Jarrett