2012-10-15 20 views
6

के साथ मैं एक छोटे थ्रेडिंग उदाहरण (लिनक्स पर) को ठीक से संकलित करने के लिए स्कैन नहीं प्राप्त कर सकता।C++ संकलन std :: थ्रेड उदाहरण scons

अगर मैं SCons चलाने के लिए, यह इस करता है:

[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 चलाएँ:

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 एनएस प्रोग्राम (धागे, आदि के लिए कुछ आउटपुट करता है)।

यहाँ मेरी 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; 
} 

किसी को भी किसी भी विचार मैं गलत क्या कर रहा है ???

किसी भी मदद की सराहना करें!

चीयर्स टिप्पणी के लिए

जैरेट

+3

क्या आपको लिंकर झंडे में '-pthread' भी जोड़ना नहीं है? –

+1

@ जोचिमपिलबोर्ग यदि लिंकिंग और संकलन दो चरणों में किया जाता है, तो हाँ। – inf

उत्तर

5

@Joachim के लिए धन्यवाद और @bamboon। लिंकर (स्कैन लाइब्रेरी) झंडे में pthread जोड़ना काम किया।

नई 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