Using openmp on windows with mingw. Cannot find -lpthread

I was finally able to get things working.

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 normal, but I also had to make sure I had the OpenMP_CXX_FLAGS 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})

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 you are configuring cmake, then that means you don't have the necessary libraries, or at least cmake can't find them. If you have the libraries and cmake can't find them, then make sure the find path is set:

set (CMAKE_FIND_ROOT_PATH C:/MinGW)

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

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

I'm using Fedora 19 and 20, and some packages relevant to your question are (for 32-bit):

mingw32-filesystem
mingw32-libgomp