C++ 编译 std::线程示例与 scons.
c++ compile std::thread example with scons
我无法让 scons 正确编译一个小线程示例(在 Linux 上)。
如果我运行 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;
}
任何人都知道我做错了什么???
感谢任何帮助!
干杯
加里特
感谢@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)
再次感谢!
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- C++ 编译 std::线程示例与 scons.