C ++ compiling std :: thread example with scons

I cannot get scons to compile a small streaming example (on Linux) correctly.

If I run scons, it does the following:

jarrett@jarrett-laptop :~/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. 

then if I run ./build/c++threads , it throws this error:

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

If I compile from the command line using this:

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

it compiles to a.out , and if I run a.out , it launches the program (draws some output for streams, etc.).

Here is my SConstruct file:

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

and here is the cpp file:

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

Does anyone know what I'm doing wrong?

Appreciate any help!

Greetings

Jarrett

+6
source share
1 answer

Thanks to @Joachim and @bamboon for comments. Added pthread files to the scons library.

The new scons file now:

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

Thanks again!

+5
source

Source: https://habr.com/ru/post/927775/


All Articles