现在最新版的C、C++、Fortran编译器基本上都内置OpenMP支持。
比如gcc、g++、gfortran(GCC套件4.2版之后开始支持) Intel C++ compiler、Intel Fortran
compiler Microsoft visual C++ (版本8.0或者叫2005之后开始支持)
一、测试Demo
#OpenMPtest.pro
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
#main.cpp
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
void Hello(void);
int main()
{
int thread_count = omp_get_num_procs() ;
# pragma omp parallel num_threads(thread_count)
Hello();
return 0;
}
void Hello(void){
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
printf("hello from thread %d of %d \n",my_rank,thread_count);
}
编译报错:
release/main.o:main.cpp:(.text+0x5): undefined reference to `omp_get_thread_num'
release/main.o:main.cpp:(.text+0xc): undefined reference to `omp_get_num_threads'
release/main.o:main.cpp:(.text.startup+0xc): undefined reference to `omp_get_num_procs'
二、解决办法
在.pro中加入:
QMAKE_LFLAGS += -fopenmp和
QMAKE_CXXFLAGS+= -fopenmp
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_LFLAGS += -fopenmp
QMAKE_CXXFLAGS+= -fopenmp
SOURCES += \
main.cpp
三、运行结果
hello from thread 4 of 8
hello from thread 1 of 8
hello from thread 2 of 8
hello from thread 0 of 8
hello from thread 3 of 8
hello from thread 6 of 8
hello from thread 5 of 8
hello from thread 7 of 8