简体   繁体   English

在Windows上使用CLION用openMP编译程序

[英]Compiling program with openMP with CLION on Windows

I have downloaded openMP using cygwin which i use as compiler in CLION. 我已经使用cygwin下载了openMP,我在CLION中将其用作编译器。

I have included 我已经包括在内

#include <omp.h>

and used its schemas eg 并使用了其模式,例如

#pragma omp paraller
        #pragma omp single

however when i use omp_get_max_threads() it throws 但是,当我使用omp_get_max_threads()它会抛出

undefined reference to `omp_get_max_threads' 未定义对`omp_get_max_threads'的引用

error, i tried to add compiler arguments: 错误,我尝试添加编译器参数: 在此输入图像描述

But the error remains the same. 但是错误仍然相同。 Is there a way how to fix this? 有没有办法解决这个问题? Thanks for help. 感谢帮助。

CLion uses CMake as a build system, OpenMP support was drastically improved in CMake 3.9+ . CLion使用CMake作为构建系统,在CMake 3.9+中大大改善了对OpenMP的支持。 Now, you need to configure your CMakeLists.txt file as follow: 现在,您需要配置CMakeLists.txt文件,如下所示:

For example, consider the example code below. 例如,考虑下面的示例代码。

OpenMP code OpenMP代码

#include <iostream>
#include <omp.h>

int main()
{
#pragma omp parallel num_threads(3)
    {
        int id = omp_get_thread_num();
        std::cout << "Greetings from process " << id << std::endl;
    }
    std::cout << "parallel for ends " << std::endl;
    return EXIT_SUCCESS;
}

CMakeLists.txt 的CMakeLists.txt

The CMakeLists.txt file will be like : CMakeLists.txt文件将类似于:

cmake_minimum_required(VERSION 3.9)
project(openmp_test) # you can change the project name

find_package(OpenMP)

add_executable(openmp_para_test main.cpp)

if(OpenMP_CXX_FOUND)
    target_link_libraries(openmp_para_test PUBLIC OpenMP::OpenMP_CXX)
endif()

If you are not familiar with CMake, Here a quick CMake tutorial to use with CLion . 如果您不熟悉CMake,请参阅此处与CLion一起使用的快速CMake教程

I'm not a user of CLION, but are the Program arguments really the arguments for the compiler for your compiled code? 我不是CLION的用户,但是Program参数确实是您编译后的代码的编译器参数吗?

Usually, when compiling an OpenMP source file, the -fopenmp flag need to be added to both the compiler and linker. 通常,在编译OpenMP源文件时,需要将-fopenmp标志同时添加到编译器和链接器中。 So, for instance: 所以,例如:

gcc -fopenmp -c foo.c -o foo.o
gcc -fopenmp -c bar.c -o bar.o
gcc -fopenmp -o app.exe foo.o bar.o

Or if you comoile and link using just one source file: 或者,如果您仅使用一个源文件就可以进行链接和链接:

gcc -fopenmp -c fooc -o app.exe

PS: There's a typo, it should be #pragma omp parallel instead of #pragma omp paraller . PS:有一个错字,应该是#pragma omp parallel而不是#pragma omp paraller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM