英特尔C ++编译器:如何在宏定义/定义中编译和链接openmp编译指示?

问题描述

我有一个简单的代码,在宏定义中使用了编译指示。使用QtMinGW可以很好地编译和运行代码。

#include <omp.h>
#include <stdio.h>

#define NUMEL 1024

#define OMP_PARALLEL _Pragma("omp parallel")
#define OMP_FOR _Pragma("omp for")

#define main_func(par)                                      \
int main(){                                                 \
    int a[NUMEL];                                           \
    OMP_PARALLEL                                            \
    {                                                       \
        int i;                                              \
        OMP_FOR                                             \
        for (i=0; i<NUMEL; i++){                            \
            printf("THRD : %d \n",omp_get_thread_num());   \
            a[i] = tan(i*2);                                \
        }                                                   \
    }                                                       \
    return 0;                                               \
}

main_func(par)

但是,如果我使用Intel C ++编译器(ICC)(在Windows上为icl.exe),则会输出以下错误:

 error: expected a ";"
  main_func(par)

通过尝试和错误,我注意到如果添加;在下面的行中,错误更改为链接错误:

#define OMP_PARALLEL _Pragma("omp parallel");
#define OMP_FOR _Pragma("omp for");

链接错误

test_omp.obj : error LNK2019: unresolved external symbol _Pragma referenced in function main
test_omp.exe : fatal error LNK1120: 1 unresolved externals
make: *** [Makefile:16: test_omp] Error 1120

任何评论都非常感谢。

解决方法

经过反复尝试,在icc论坛上进行了一些问答,结果证明

  1. _Pragma应该替换为__pragma
  2. 不需要引号

因此,在宏定义中使用omp编译指示的intel / icc语法应按以下方式更正:

#define OMP_PARALLEL __pragma(omp parallel)
#define OMP_FOR __pragma(omp for)

我希望它对其他人有帮助...我留下了测试代码和makefile,因此您也可以尝试一下。我花了三天的时间来解决这个问题。

如果对我的问题有帮助,请随时对其投票。

#include <omp.h>
#include <stdio.h>

#define NUMEL 1024

#define OMP_PARALLEL __pragma(omp parallel)
#define OMP_FOR __pragma(omp for)

#define main_func(par)                                      \
int main(){                                                 \
    int a[NUMEL];                                           \
    OMP_PARALLEL                                            \
    {                                                       \
        int i;                                              \
        OMP_FOR                                             \
        for (i=0; i<NUMEL; i++){                            \
            printf("THRD : %d \n",omp_get_thread_num());   \
            a[i] = tan(i*2);                                \
        }                                                   \
    }                                                       \
    return 0;                                               \
}

main_func(par)


int main2(){
    int a[NUMEL];
    #pragma omp parallel
    {
        int i;
        #pragma omp for
        for (i=0; i<NUMEL; i++){
            printf("THRD : %d \n",omp_get_thread_num());
            a[i] = tan(i*2);
        }
    }
    return 0;
}

PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CXX=icl
CC=icl
LD =xilink

LIBDIR=Debug/lib/
OBJDIR=Debug/obj/

CFLAGS=/Qopenmp
LDFLAGS= /nodefaultlib:vcomp libiomp5md.lib
OBJS = $(OBJDIR)test_omp.obj 

all: test_omp

test_omp: $(OBJS)  
    $(LD) $(LDFLAGS) $(OBJS) -Fe:test_omp.exe


$(OBJDIR)%.obj: $(PROJECT_ROOT)%.cpp
    $(CXX) -c $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -Fo:$@ $<

$(OBJDIR)%.obj: $(PROJECT_ROOT)%.c
    $(CC) $(CFLAGS) $(CPPFLAGS) -Fo:$@ -c $<


clean:
    rm -fr test_icl $(OBJS) test_omp.exe

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...