Using openmp in windows with mingw. Could not find -lpthread

I have a CMake project that uses OpenMP and works with linux. When I went to compile it on my Windows machine, it looked like CMake was having trouble finding openmp flags for mingw gcc.

I decided to try a smaller test case and just compile main_openmp.c

#include <omp.h> #include <stdio.h> int main(int argc, char* argv[]) { int id; #pragma omp parallel private(id) { id = omp_get_thread_num(); printf("%d: Hello World!\n", id); } return 0; } 

Then when I try to compile

 gcc -o OpenMPTest2 main_testomp.c -fopenmp 

I get

 >>> gcc -o OpenMPTest2 main_testomp.c -fopenmp c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lpthread collect2.exe: error: ld returned 1 exit status 

I tried to find a solution online and tried the options -lgomp, -lpthreadgc2 and -lpthreadvc2 without any improvement.

I searched the C: \ MinGw directory recursively for any file names containing lpthread and got this:

 C:\MinGW\bin\pthreadgc2.dll C:\MinGW\bin\pthreadgce2.dll C:\MinGW\var\cache\mingw-get\packages\pthreads-w32-2.9.1-1-mingw32-dll.tar.lzma C:\MinGW\var\lib\mingw-get\data\mingw32-pthreads-w32.xml 

I am not sure if I am missing a flag, package or what I am doing wrong. For good measure, here is the output of gcc -v

 Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --ena ble-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32 -registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gm p-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-sy stem-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv -prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIM E_T Thread model: win32 gcc version 4.8.1 (GCC) 

Any idea what's wrong?

+7
c ++ gcc windows openmp mingw
source share
3 answers

Finally, I was able to get everything to work.

First, using mingw-get, I installed mingw32-pthreads-w32

This allowed me to use the -fopenmp flag with gcc.

But when using CMake, I had to include the lines:

 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! x_x") ENDIF() 

as usual, but I also had to make sure that I have the OpenMP_CXX_FLAGS command in my target_link_libraries command

 set(SOURCE_FILES src/foo.cpp src/bar.cpp src/baz.cpp) add_library(<mylib> SHARED ${SOURCE_FILES}) target_link_libraries(<mylib> ${OpenMP_CXX_FLAGS}) 
+18
source share

In your CMakeLists.txt file, you should use the following:

 find_package(OpenMP REQUIRED) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") 

If you get an error when configuring cmake, it means that you do not have the necessary libraries or at least cmake cannot find them. If you have libraries and cmake cannot find them, make sure the search path is set:

 set (CMAKE_FIND_ROOT_PATH C:/MinGW) 

You might want to create a "toolchain" file as described here .

I also suggest you try mingw-w64 for cross-compiling for Windows. I use it for both 32-bit and 64-bit applications. I use mingw [32/64] -cmake and then everything works.

I am using Fedora 19 and 20, and some packages related to your question (for 32-bit):

 mingw32-filesystem mingw32-libgomp 
+1
source share

Very simple steps that worked for me:

  • Came to the installation manager MinGW
  • Locate the MinGW standard library section on the left side.
  • Install the mingw32-pthreads-w32 packages .
+1
source share

All Articles