问题
I am newbie in openmp. The following is the environment.
OS : Mac OSX Mavericks
Compiler : gcc (MacPorts gcc48 4.8.2_0) 4.8.2
IDE : Eclipse Kepler CDT plugin
I wrote the following openmp program
#include < stdio.h>
#include < omp.h>
int main()
{
#pragma omp parallel
{
int i=omp_get_thread_num();
printf("hello (%d)",i);
printf("world (%d)",i);
}
}
I compiled the above program and got the error that omp.h is not found and lgomp not found. Hence I added in the project properties an include path with /opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.2/include and a library path /opt/local/lib/gcc48. The include path had the omp.h file and the library path had the file libomp.o.
I include the -fopenmp option in both the linker and the compiler option through project properties. It is compiling with gcc -I/opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.2/include -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/OpenMPCourseExamples.d" -MT"src/OpenMPCourseExamples.d" -o "src/OpenMPCourseExamples.o" "../src/OpenMPCourseExamples.c" and linking with the command "gcc -L/opt/local/lib/gcc48 -fopenmp -o "OpenMPCourseExamples" ./src/OpenMPCourseExamples.o".
With the above command it compiles without an error but with a warning - "warning: unknown pragma ignored [-Wunknown-pragmas] #pragma omp parallel".
Also, I set an environment variable in the launch properties with OMP_NUM_THREADS=4. I ran the program that compiled with the above warning. I am getting only "hello (0)world (0)". I was under the impression that I should start four threads and should see the other outputs of "hello(1)world(1)hello(2)world(2)hello(3)world(3)" in some ordering as well. Now, here are my following questions.
- Why am I getting the #pragma warning?
- Is the compiler really detecting the openmp and building with openmp?
- If everything is correct, why am I not seeing four different threads getting started?
回答1:
MacPorts configures the GCC build process with --program-suffix=-mp-${major}
and therefore all compiler executables have the -mp-4.8
suffix. When you call gcc
, you end up using Apple's Clang compiler, which does not support OpenMP and therefore does not recognise the -fopenmp
option and #pragma omp ...
.
You have to do the following changes to the project settings:
- Change the compiler command to
gcc-mp-4.8
- Change the linker command to
gcc-mp-4.8
- Remove the explicit specification of the include and library paths since the presence of
-fopenmp
adds them automatically.
回答2:
The final steps that worked for openmp, macports gcc compiler, eclipse CDT in mac osx mavericks are.
- Enable "Make ToolChain(s) Preferred" in Eclipse->Preference->C/C++->New C/C++ Project Wizard.
- sudo port select --list gcc and set it sudo port select --set gcc with mp-gcc.
- File->New Project->C Project (not C++) and create a hello world project.
- In Project->Properties->C/C++ Build->Settings->Tool Settings set the following. (a) GCC C Compiler to /opt/local/bin/gcc-mp-4.8 (b)MAC OSX Linker to /opt/local/bin/gcc-mp-4.8
- Build the hello world project and make sure, it compiles and runs successfully.
- Include the open mp code. The code asked in the question of this page.
- Go to again Project->Properties->C/C++ Build->Settings->Tool Settings set the following. (a) GCC Compiler ->Miscellaneous add -fopenmp (b) MacOSx Linker->Miscellaneous set -fopenmp
- Build the code again.
The above steps worked good for me.
来源:https://stackoverflow.com/questions/20489813/compiling-openmp-macports-gcc-and-eclipse-cdt