简体   繁体   中英

Compiling program with openMP with CLION on Windows

I have downloaded openMP using cygwin which i use as compiler in 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

undefined reference to `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+ . Now, you need to configure your CMakeLists.txt file as follow:

For example, consider the example code below.

OpenMP code

#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

The CMakeLists.txt file will be like :

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 .

I'm not a user of CLION, but are the Program arguments really the arguments for the compiler for your compiled code?

Usually, when compiling an OpenMP source file, the -fopenmp flag need to be added to both the compiler and linker. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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