2012-10-15 12 views
6

Non riesco a ottenere scons per compilare correttamente un piccolo esempio di threading (su Linux).C++ compile std: esempio di thread con scons

Se corro scons, lo fa:

[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. 

poi se corro ./build/c++threads getta questo errore:

terminate called after throwing an instance of 'std::system_error' 
    what(): Operation not permitted 
Aborted 

Se compilo dalla riga di comando con questo:

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

compila a a.out e se corro a.out it ru ns il programma (fa un po 'di output per i thread, ecc.).

Ecco il mio file 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) 

e c'è il file cpp qui:

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

Qualcuno ha idea di cosa sto facendo male ???

Apprezzare qualsiasi aiuto!

Acclamazioni

Jarrett

+3

Non è necessario aggiungere '-pthread' alle flag del linker? –

+1

@JoachimPileborg Se il collegamento e la compilazione avvengono in due passaggi, sì. – inf

risposta

5

Grazie a @Joachim e @bamboon per i commenti. L'aggiunta di pthread alle bandiere del linker (libreria scons) ha funzionato.

Il nuovo file scons è ora:

# 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) 

Grazie ancora!

+0

Nota: puoi anche accettare la tua risposta, per dimostrare che ha risolto il problema. – inf

+0

A quanto pare, devo aspettare 2 giorni prima di poter accettare la mia risposta: \ – Jarrett