Mac 使用 OpenMP/Clang

新建 hello.cpp 文件:c++

#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;
}

編譯會遇到以下錯誤:shell

hello.cpp:1:10: fatal error: 'omp.h' file not found
#include <omp.h>
          ^~~~~~
1 error generated.

由於默認的 g++ 編譯器不支持 openmp,咱們能夠設置 LLVM/Clang 編譯器來編譯 openmp。
執行如下命令:bash

brew install llvm # 安裝 LLVM 編譯器
brew install libomp # 安裝 OpenMP 庫
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile # 將 llvm 的可執行文件添加到 PATH 目錄

而後執行code

clang -fopenmp hello.cpp -o hello
./hello

個人CPP文件中用到了STL 中的 vector,而後就遇到了新的錯誤:ci

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 庫因此出錯了,咱們能夠顯式地連接標準庫:get

clang -fopenmp hello.cpp -o hello -lstdc++
#或者 clang -fopenmp hello.cpp -o hello -lc++
./hello

也能夠直接用 clang++:編譯器

clang++ -fopenmp hello.cpp -o hello
./hello
相關文章
相關標籤/搜索