Mac 使用 OpenMP/Clang
新建 hello.cpp 文件:
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
return 0;
}
编译会遇到如下错误:
hello.cpp:1:10: fatal error: 'omp.h' file not found
#include <omp.h>
^~~~~~
1 error generated.
因为默认的 g++ 编译器不支持 openmp,我们可以设置 LLVM/Clang 编译器来编译 openmp。
执行以下命令:
brew install llvm # 安装 LLVM 编译器
brew install libomp # 安装 OpenMP 库
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile # 将 llvm 的可执行文件添加到 PATH 目录
然后执行
clang -fopenmp hello.cpp -o hello
./hello
我的CPP文件中用到了STL 中的 vector,然后就遇到了新的错误:
Undefined symbols for architecture x86_64:
"std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
std::__1::vector<int, std::__1::allocator<int> >::assign(unsigned long, int const&) in seq-3efcea.o
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
void std::__1::vector<int, std::__1::allocator<int> >::__push_back_slow_path<int>(int&&) in seq-3efcea.o
"std::logic_error::logic_error(char const*)", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"std::length_error::~length_error()", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
_main in seq-3efcea.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::insert(unsigned long, char const*)", referenced from:
_main in seq-3efcea.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
_main in seq-3efcea.o
"std::terminate()", referenced from:
___clang_call_terminate in seq-3efcea.o
"typeinfo for std::length_error", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"vtable for std::length_error", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"operator delete(void*)", referenced from:
std::__1::__vector_base<int, std::__1::allocator<int> >::~__vector_base() in seq-3efcea.o
std::__1::__deque_base<int, std::__1::allocator<int> >::~__deque_base() in seq-3efcea.o
std::__1::__deque_base<int, std::__1::allocator<int> >::clear() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*> >::~__split_buffer() in seq-3efcea.o
std::__1::__vector_base<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::~__vector_base() in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::~__split_buffer() in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
...
"operator new(unsigned long)", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"___cxa_allocate_exception", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"___cxa_begin_catch", referenced from:
___clang_call_terminate in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*> >::shrink_to_fit() in seq-3efcea.o
"___cxa_end_catch", referenced from:
std::__1::__split_buffer<int*, std::__1::allocator<int*> >::shrink_to_fit() in seq-3efcea.o
"___cxa_free_exception", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"___cxa_throw", referenced from:
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::__split_buffer<int, std::__1::allocator<int>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int>&) in seq-3efcea.o
std::__1::deque<int, std::__1::allocator<int> >::__add_back_capacity() in seq-3efcea.o
std::__1::__split_buffer<int*, std::__1::allocator<int*>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<int*>&) in seq-3efcea.o
"___gxx_personality_v0", referenced from:
_main in seq-3efcea.o
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::vector(unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&) in seq-3efcea.o
std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::vector(std::__1::vector<int, std::__1::allocator<int> > const&) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::allocate(unsigned long) in seq-3efcea.o
std::__1::vector<int, std::__1::allocator<int> >::vector(unsigned long, int const&) in seq-3efcea.o
void std::__1::vector<int, std::__1::allocator<int> >::__push_back_slow_path<int>(int&&) in seq-3efcea.o
...
ld: symbol(s) not found for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
是因为我们编译的是 C++文件,clang 没有链接 STL 库所以出错了,我们可以显式地链接标准库:
clang -fopenmp hello.cpp -o hello -lstdc++
#或者 clang -fopenmp hello.cpp -o hello -lc++
./hello
也可以直接用 clang++:
clang++ -fopenmp hello.cpp -o hello
./hello
Mac 使用 OpenMP/Clang的更多相关文章
- 尝试在Mac/iOS上使用tcmalloc库
概述 TCMalloc 是 Google 开发的内存分配器,在不少项目中都有使用,例如在 Golang 中就使用了类似的算法进行内存分配.它具有现代化内存分配器的基本特征:对抗内存碎片. ...
- R语言︱XGBoost极端梯度上升以及forecastxgb(预测)+xgboost(回归)双案例解读
XGBoost不仅仅可以用来做分类还可以做时间序列方面的预测,而且已经有人做的很好,可以见最后的案例. 应用一:XGBoost用来做预测 ------------------------------- ...
- xgboost: 速度快效果好的boosting模型
转自:http://cos.name/2015/03/xgboost/ 本文作者:何通,SupStat Inc(总部在纽约,中国分部为北京数博思达信息科技有限公司)数据科学家,加拿大Simon Fra ...
- libc++
今天测试最新的微信iOS SDK, 仅仅是建了一个空的工程,把sdk加进去运行,就报了以下错误: Undefined symbols for architecture x86_64: "op ...
- atomic, spinlock and mutex性能比较
我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...
- [BS] 小知识点总结-02
1. dispatch_GCD 可让某操作延迟x秒执行 //模拟网速慢,延迟3s返回数据(就会导致右侧数据和左侧标签不对应) dispatch_after(dispatch_time(DISPATC ...
- 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】02-第一个C语言程序
前言 前面已经唠叨了这么多理论知识,从这讲开始,就要通过接触代码来学习C语言的语法.学习任何一门语言,首先要掌握的肯定是语法.学习C语言语法的目的:就是能够利用C语言编写程序,然后运行程序跟硬件(计算 ...
- 学习C++之前要先学习C语言吗?
C++ 读作“C加加”,是“C Plus Plus”的简称.顾名思义,C++ 是在C语言的基础上增加新特性,玩出了新花样,所以叫“C Plus Plus”,就像 iPhone 7S 和 iPhone ...
- 关于mac安装rails报错clang: error: unknown argument
文章都是从我的个人博客上转载过来的,大家可以点击我的个人博客. www.iwangzheng.com mac上安装rails的时候报错, 安装rails的在终端执行了一句命令: $sudo gem i ...
随机推荐
- Windows Tomcat 日志输出到 catlina.out 按日期输出,自启动
配置tomcat和jdk环境变量 CATALINA_BASE D:\ApacheSoftwareFoundation\Tomcat8.5 CATALINA_HOME D:\ApacheSoftware ...
- eclipse导入java工程
1)File下的import选项 2)点击General,选择Existing Projects into Workspace,点击next 3)点击Browse,在弹出的窗口中选择导入工程所在的文件 ...
- SAP MM 明明有需求,为啥MRP RUN后没有PR单据产生?
SAP MM 明明有需求,为啥MRP RUN后没有PR单据产生? 用户报了一个问题说,对于物料号42011222的采购单 4500000156建好了,为啥PR没有自动生成 . 我们检查了物料的MRP ...
- 使用VC建立网络连接并访问网络资源
目录 1. 提出问题 2. 解决方案 1. 提出问题 在windows下可以通过系统操作,将局域网的资源映射到本地,从而实现像本地数据一样访问网络资源.实际上这些步骤也可通过代码调用win32函数实现 ...
- Oracle字符到数值转换错误
[错误] [问题分析] line 3: 定义 NUM_VAL varchar2(500); line 9: NUM_VAL := 'NUM'+1; NUM_VAL是一个varchar类型的数据,而在数 ...
- 逐个使用C++11新特性
C++11 auto & decltype auto:根据变量初始值来推导变量类型,并用右值初始化变量. decltype:从表达式推导出类型,并将变量定义为该类型,但不用表达式的值初始化该变 ...
- SpringBoot+MyBatis配置多数据源
SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...
- EF Code First 连接MySql
看了很多文章,尝试了很多次总是进行不下去,整理一下,以便日后查看. 1.创建ASP.NET MVC项目(EFCodeFirst) 1.1.右键点击引用选择管理NuGet程序包下载MySql.Data. ...
- memcached架构及缓存策略
----------------------------------------概述---------------------------------------- Memcached是一套高性能分布 ...
- SharpZipLib压缩解压的使用
项目中使用 Velocity 将模板和生成的动态内容(HTML.XML等)合并保存到redis数据库中,考虑到压缩的文件容量会比较小,方便传输而且存储所使用的空间也会比较小,所以要压缩一下,读取的时候 ...