I am trying to run simple OpenMP
program in CLion IDE
.
When I run it I get an ERROR:
CMakeFiles\openmp_test_clion.dir/objects.a(main.cpp.obj): In function `main':
D:/.../openmp_test_clion/main.cpp:9: undefined reference to 'omp_get_thread_num'
collect2.exe: error: ld returned 1 exit status
Here is my code:
#include <stdio.h>
#include <omp.h>
int main()
{
int id;
#pragma omp parallel private(id)
{
id = omp_get_thread_num();
printf("%d: Hello World!\n", id);
}
return 0;
}
Here is my CMakeLists.txt
:
cmake_minimum_required(VERSION 3.6)
project(openmp_test_clion)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(openmp_test_clion ${SOURCE_FILES})
message(STATUS "Checking OpenMP")
find_package(OpenMP)
IF(OPENMP_FOUND)
message("Found OpenMP!)
# add flags for OpenMP
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
ELSE()
message("Missed OpenMP!")
ENDIF()
Here is screen of my toolchains:
I have zero experience with OpenMP
and I am beginner programmer in C++
so please give me a bit of an explanation how to setup my project.
Create a new projectIf no project is currently opened in CLion, click New Project on the Welcome screen. Otherwise, select File | New Project on the main menu. In the New Project dialog that opens, select the target type of your project (executable or library), and the language to be used (pure C or C++).
CLion supports and auto-detects Microsoft Visual C++ toolchain from Visual Studio 2013, 2015, 2017, and 2019. To use Clang-cl instead of MSVC, select the Visual Studio toolchain in CLion and point to clang-cl.exe in the toolchain settings (versions 8.0 and later are supported).
So after a while I figured it out. I changed CmakeLists.txt as following:
cmake_minimum_required(VERSION 3.6)
project(openmp_test_clion)
# added -fopenmp
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")
set(SOURCE_FILES main.cpp)
add_executable(openmp_test_clion ${SOURCE_FILES})
And I needed to install openmp via TDM-GCC installer .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With