Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compile std::thread example with scons

I can't get scons to properly compile a small threading example (on Linux).

If I run scons, it does this:

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 with this:

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

it compiles to a.out, and if I run a.out it runs the program (does some output for threads, etc).

Here's 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's 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;
}

Anyone have any idea what I'm doing wrong???

Appreciate any help!

Cheers

Jarrett

like image 868
Jarrett Avatar asked Oct 15 '12 18:10

Jarrett


People also ask

What is a basic C++ project that uses SCons?

This post is a short example of a basic C++ project, that uses SCons. The purpose of this basic example is to set a baseline. It demonstrates how to build a non-trivial C++ project with out-of-the-box SCons. Future posts in my SCons series will describe various extensions to SCons, so it is important to be able to compare to a simple baseline.

What is SCons?

SCons is an open source software construction tool – a next generation build tool. I have previously written an introduction to SCons. This post is a short example of a basic C++ project, that uses SCons.

What is a thread in C++ example?

Example. In C++, threads are created using the std::thread class. A thread is a separate flow of execution; it is analogous to having a helper perform one task while you simultaneously perform another. When all the code in the thread is executed, it terminates.

How many c++ (cpp) thread examples are there?

C++ (Cpp) std::thread - 30 examples found. These are the top rated real world C++ (Cpp) examples of std::thread extracted from open source projects. You can rate examples to help us improve the quality of examples.


1 Answers

Thanks to @Joachim and @bamboon for the comments. Adding pthread to the linker (scons library) flags worked.

The new scons file is 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!

like image 111
Jarrett Avatar answered Oct 15 '22 09:10

Jarrett