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 derlemekterminate 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çinJarrett
Ayrıca, bağlayıcı bayraklarına '-pthread' eklemeniz gerekmiyor mu? –
@JoachimPileborg Bağlantı ve derleme iki adımda yapılırsa, evet. – inf