新建 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的更多相关文章

  1. 尝试在Mac/iOS上使用tcmalloc库

    概述        TCMalloc 是 Google 开发的内存分配器,在不少项目中都有使用,例如在 Golang 中就使用了类似的算法进行内存分配.它具有现代化内存分配器的基本特征:对抗内存碎片. ...

  2. R语言︱XGBoost极端梯度上升以及forecastxgb(预测)+xgboost(回归)双案例解读

    XGBoost不仅仅可以用来做分类还可以做时间序列方面的预测,而且已经有人做的很好,可以见最后的案例. 应用一:XGBoost用来做预测 ------------------------------- ...

  3. xgboost: 速度快效果好的boosting模型

    转自:http://cos.name/2015/03/xgboost/ 本文作者:何通,SupStat Inc(总部在纽约,中国分部为北京数博思达信息科技有限公司)数据科学家,加拿大Simon Fra ...

  4. libc++

    今天测试最新的微信iOS SDK, 仅仅是建了一个空的工程,把sdk加进去运行,就报了以下错误: Undefined symbols for architecture x86_64: "op ...

  5. atomic, spinlock and mutex性能比较

    我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...

  6. [BS] 小知识点总结-02

    1.  dispatch_GCD 可让某操作延迟x秒执行 //模拟网速慢,延迟3s返回数据(就会导致右侧数据和左侧标签不对应) dispatch_after(dispatch_time(DISPATC ...

  7. 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】02-第一个C语言程序

    前言 前面已经唠叨了这么多理论知识,从这讲开始,就要通过接触代码来学习C语言的语法.学习任何一门语言,首先要掌握的肯定是语法.学习C语言语法的目的:就是能够利用C语言编写程序,然后运行程序跟硬件(计算 ...

  8. 学习C++之前要先学习C语言吗?

    C++ 读作“C加加”,是“C Plus Plus”的简称.顾名思义,C++ 是在C语言的基础上增加新特性,玩出了新花样,所以叫“C Plus Plus”,就像 iPhone 7S 和 iPhone ...

  9. 关于mac安装rails报错clang: error: unknown argument

    文章都是从我的个人博客上转载过来的,大家可以点击我的个人博客. www.iwangzheng.com mac上安装rails的时候报错, 安装rails的在终端执行了一句命令: $sudo gem i ...

随机推荐

  1. .net 配置swagger

    第一步: 在nuget.org中查找Swashbuckle并下载 在nuget.org中查找Swagger.net.UI,并下载 第二步: 下载完之后,App_Start多了三个文件 Swagger. ...

  2. Gerrit系统框架介绍

    Gerrit目录介绍 转自:https://blog.csdn.net/tanshizhen119/article/details/79889242 先上图 bin/ : 主要是放gerrit.sh启 ...

  3. Java设计模式---Strategy策略模式

    参考于 : 大话设计模式 马士兵设计模式视频 1.场景介绍 购物网站上有一个产品,有三个字段,档次,价格,重量. 有些同学喜欢轻的,有些手头紧,想要便宜的,有些喜欢档次高的. 那么我们为了提高网站用户 ...

  4. 在docker中初次体验.net core 2.0

    .net core的跨平台有了Linux,不能没有docker……网上的系列文章一大推,特别是docker还有了中文官网:https://www.docker-cn.com/ .上面说的很清楚了,这里 ...

  5. SharePoint代码建表(实战)

    分享人: 广州华软 无名 一. 前言 虽然SharePoint提供可视化界面建表,但是,不利于开发自动化部署.通常,如果通过手动建表,我们先在测试环境建表,然后存为模板,再上传到实际环境,最后根据模板 ...

  6. Git安装教程(windows)

    Git是当今最流行的版本控制软件,它包含了许多高级工具,这里小编就讲一下Git的安装. 下载地址:https://git-scm.com/downloads 首先如下图:(点击next) 第二步:文件 ...

  7. Salesforce 简介

    Salesforce是什么 Salesforce是一个功能全面的云平台.它是践行Saas(软件及服务)概念的先驱之一. Salesforce的核心功能是CRM(客户关系管理系统).系统默认提供大多数C ...

  8. Linux使用wget、安装Python

    原文:https://blog.csdn.net/ghost_chou/article/details/81905302 1.指定需要安装的目录或创建并访问到该选择的目录下. mkdir /data/ ...

  9. 设计模式系列之观察者模式(Observer Pattern)

    意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 主要解决:一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作 ...

  10. 服务器部署Apache+PHP+MYSQL+Laravel

    1.安装PHP 添加php安装源: sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej ...