如何在Windows上使用MinGW安装/启用OpenMP for Eclipse ?



当我继续学习c++时,我正在尝试使用MinGW工具链在Eclipse (C/c++ Mars 4.5.0)中启用OpenMP。我只想说我不知道该怎么做。在项目设置中将-fopenmp标志添加到C/c++编译器选项中是不够的。我尝试使用GNU Make Builder编译和运行OpenMP提供的以下测试代码:

#include <omp.h>
#include <stdio.h>
int main()
{
    #pragma omp parallel printf("Hello from thread %d, nthreads %dn", omp_get_thread_num(), omp_get_num_threads());
}

Eclipse输出如下内容:

Building file: ../OpenMPTest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"OpenMPTest.d" -MT"OpenMPTest.d" -o "OpenMPTest.o" "../OpenMPTest.cpp"
../OpenMPTest.cpp: In function 'int main()':
../OpenMPTest.cpp:14:23: error: expected '#pragma omp' clause before 'printf'
  #pragma omp parallel printf("Hello from thread %d, nthreads %dn", omp_get_thread_num(), omp_get_num_threads());
                       ^
../OpenMPTest.cpp:15:1: error: expected primary-expression before '}' token
 }
 ^
make: *** [OpenMPTest.o] Error 1

这给了我一个(可能不正确的)印象,即Eclipse找不到OpenMP库。

所以,在我搞砸任何无法修复的东西之前,我想知道我是否可以得到一个傻瓜一步一步的指南,在Eclipse项目中使用MinGW安装和启用OpenMP。

不要在#pragma行上放置任何不应该在那里的东西。#pragma行是预处理器的指令,您的printf代码不应该在那里。

你的main()应该看起来像

int main()
{
    #pragma omp parallel
    {
        printf("Hello from thread %d, nthreads %dn", omp_get_thread_num(), omp_get_num_threads());
    }
    return 0;
}

最新更新