2012-10-15 20 views
6

Küçük bir iş parçacığı örneği (Linux'ta) düzgün bir şekilde derlemek için işaretler alamıyorum. SonraC++ derleme std :: iş parçacığı örneği scons ile

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

Ben ./build/c++threads çalıştırırsanız bu hatayı atar:

Ben scons çalıştırırsanız, Bu işi yapan

ben bu komut satırından derlemek
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'u derler ve a.out çalıştırırsam ns programı (thread, vb. için bazı çıktılar yapar).

İşte benim SConstruct dosyasıdır:

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

ve burada cpp dosyası var:

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

Herkes ben yanlış yapıyorum bir fikrin var ???

Herhangi bir yardım için teşekkür ederiz!

Şerefe yorumlar için

@Joachim için

Jarrett

+3

Ayrıca, bağlayıcı bayraklarına '-pthread' eklemeniz gerekmiyor mu? –

+1

@JoachimPileborg Bağlantı ve derleme iki adımda yapılırsa, evet. – inf

cevap

5

Teşekkür ve @bamboon. Linker (scons kütüphanesi) bayrakları pthread ekleyerek çalıştı.

yeni scons dosyası

artık şudur: Yine

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

teşekkürler!

+0

Not: Sorunu çözdüğünü göstermek için kendi cevabınızı da kabul edebilirsiniz. – inf

+0

Görünüşe göre, kendi cevabımı kabul edebilmem için 2 gün beklemem gerekiyor: – Jarrett

İlgili konular