6

小さなスレッド化の例 (Linux) を適切にコンパイルするための scons を取得できません。

scons を実行すると、次のようになります。

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.

次に、実行すると、次の./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とプログラムが実行されます(スレッドなどの出力を行います)。

ここに私の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;
}

誰が私が間違っているのか知っていますか???

どんな助けにも感謝します!

乾杯

ジャレット

4

1 に答える 1

6

@Joachim と @bamboon のコメントに感謝します。pthread をリンカ (scons ライブラリ) フラグに追加すると機能しました。

新しい 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)

再度、感謝します!

于 2012-10-15T18:28:01.120 に答える