Qt Pluging with OpenMP Support on MinGW: Undefined reference?-Collection of common programming errors

I develop “Qt Plugin” with uses OpenMP support (OpenMP support can be enable/disable using CMake parameter). I use MinGW and CMake as development environment. This is how I enable/disable OpenMP in the code :

#ifdef OPENMP_ENABLE
#pragma omp parallel for
#endif  for(int i=0; igetZSize(); i++){  .

I have enabled OpenMP In CMake file as follows:

OPTION (OPENMP_SUPPORT  "Build with OpenMP parallaization enabled")

IF (OPENMP_SUPPORT)
 FIND_PACKAGE( OpenMP )
 SET(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS}")

 IF ( OpenMP_CXX_FLAGS )
  MESSAGE("------- Adding compiler parameter for OpenMP")
  ADD_DEFINITIONS(-DOPENMP_ENABLE)
 ENDIF()
ENDIF()

and to build Qt plugin, I have configured CMake as follows :

ADD_DEFINITIONS(-DQT_PLUGIN)

ADD_DEFINITIONS(-DQT_SHARED)

This configuration compiles fine with linux. In windows it compiles fine WITHOUT OpenMP support. But when build with OpenMp support , it gives the error

c:/mingw4/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning:auto-importing has been activated without –enable-auto-import specified on the command line. This should work unless it involves constant data structures referencing symbol from auto-imported DLLs. CMakeFiles\RinzoDLPluginIPThreshold.dir\ui\dialogthresholdconfig.cpp.obj:C:/svnosaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/imageprocplugins/thresholdPlgin/ui/dialogthresholdconfig.cpp:221: undefined reference to GOMP_parallel_stat' CMakeFiles\RinzoDLPluginIPThreshold.dir\ui\dialogthresholdconfig.cpp.obj:C:/svnosaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/imageprocplugins/thresholdPlgin/ui/dialogthresholdconfig.cpp:221: undefined reference to GOMP_parallel_end

CMakeFiles\RinzoDLPluginIPThreshold.dir\ui\dialogthresholdconfig.cpp.obj: In fuction ZN21DialogThresholdConfig9slotApplyEv.omp_fn.0': C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/imageprocplugins/threholdPlugin/ui/dialogthresholdconfig.cpp:223: undefined reference to omp_get_nu_threads’ C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/imageprocplugins/threholdPlugin/ui/dialogthresholdconfig.cpp:223: undefined reference to omp_get_thead_num' C:/svn/osaka3d/trunk/osaka3d/rinzo-platform/src/dlplugins/imageprocplugins/threholdPlugin/ui/dialogthresholdconfig.cpp:233: undefined reference to omp_get_thead_num’ collect2: ld returned 1 exit status make[2]: * [dist/plugins/libRinzoDLPluginIPThreshold.dll] Error 1

Here is my complete CMake file: http://www.keepandshare.com/doc/view.php?id=2552392&da=y

Any tip?

  1. OpenMP requires a runtime library (called libgomp in case of gcc), which must be linked into the created executable. It is therefore not sufficient to add the OpenMP flags to the compilation flags, they must also be added to the link flags:

    set_target_properties( LINK_FLAGS "${OpenMP_CXX_FLAGS}")
    

    Of course, you need to replace with the actual name of your target.

    And by the way, and extra definition like OPENMP_ENABLE is superfluous. If OpenMP is enabled, the macro _OPENMP is implicitly defined to contain the supported version of OpenMP (more precisely, the release date of the supported standard). You can use this macro to test for OpenMP support in the source code:

    #if defined(_OPENMP)
    // use openmp
    #endif
    

Originally posted 2013-11-09 23:13:39.