如何使用`openmp`编译这个简单的C ++代码

时间:2016-12-05 00:03:27

标签: c++ macos gcc compilation compiler-errors

这是代码

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

来自Installing OpenMP on Mac OS X 10.11的@prakharjain回复的复制粘贴。

使用gcc-6 -fopenmp -o a a.cpp进行编译返回

Undefined symbols for architecture x86_64:
  "___gxx_personality_v0", referenced from:
      Dwarf Exception Unwind Info (__eh_frame) in ccyHyU08.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

请注意,如果我注释掉#pragma omp parallel,那么它编译得很好。我按照Installing OpenMP on Mac OS X 10.11的解释,按照@AlejandroDanielNoel技术安装了gcc-6。我在Mac OS X 10.12.1上。我的机器上有4个逻辑核心。

你能帮我编译一下这个简单的代码吗?

使用gcc-6 -v -fopenmp -o a a.cpp

进行编译时,这是整个输出
Using built-in specs.
COLLECT_GCC=gcc-6
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/lto-wrapper
Target: x86_64-apple-darwin16.1.0
Configured with: ../configure --build=x86_64-apple-darwin16.1.0 --prefix=/usr/local/Cellar/gcc/6.2.0 --libdir=/usr/local/Cellar/gcc/6.2.0/lib/gcc/6 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-6 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc 6.2.0 --without-multilib' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk
Thread model: posix
gcc version 6.2.0 (Homebrew gcc 6.2.0 --without-multilib) 
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
 /usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/cc1plus -quiet -v -D__DYNAMIC__ a.cpp -fPIC -quiet -dumpbase a.cpp -mmacosx-version-min=10.12.1 -mtune=core2 -auxbase a -version -fopenmp -o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccZjzRvK.s
GNU C++14 (Homebrew gcc 6.2.0 --without-multilib) version 6.2.0 (x86_64-apple-darwin16.1.0)
    compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/local/include"
ignoring nonexistent directory "/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../x86_64-apple-darwin16.1.0/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0
 /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0/x86_64-apple-darwin16.1.0
 /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0/backward
 /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/include
 /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/include-fixed
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks
End of search list.
GNU C++14 (Homebrew gcc 6.2.0 --without-multilib) version 6.2.0 (x86_64-apple-darwin16.1.0)
    compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 20fdc54fe5944e0048cbe59ee8edcdd5
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
 as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccZjzRvK.s
COMPILER_PATH=/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/
LIBRARY_PATH=/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
 /usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/collect2 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/ -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0 -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../.. /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o -lgomp -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 6.2.0
/usr/bin/ld -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/ -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0 -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../.. /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o -lgomp -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
@(#)PROGRAM:ld  PROJECT:ld64-274.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
    /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0
    /usr/local/Cellar/gcc/6.2.0/lib/gcc/6
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/lib
Framework search paths:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/
Undefined symbols for architecture x86_64:
  "___gxx_personality_v0", referenced from:
      Dwarf Exception Unwind Info (__eh_frame) in ccH3Ixr9.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:2)

只需尝试g++-6而不是gcc-6