MantisBT - CMake
View Issue Details
0015392CMakeCMakepublic2015-02-09 10:582016-06-10 14:31
Mathieu Malaterre 
Kitware Robot 
normalminorhave not tried
closedmoved 
CMake 3.0.2 
 
0015392: Unclear documentation for module: FindOpenMP.cmake
The FindOpenMP module states:

[...]
       The following variables are set:

          OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
[...]

Instead is should be stated that the OpenMP flags should be passed both to the C compiler and the linker.

Eg, the following will not work (linker step will fail)

cmake_minimum_required(VERSION 2.8)
project(omp C)
find_package(OpenMP REQUIRED)
# OPENMP_FOUND

# Not OK
# pass OpenMP flag to C compiler (only)
set_property(SOURCE tomp.c
  PROPERTY COMPILE_FLAGS ${OpenMP_C_FLAGS}
  )

# OK
# pass OpenMP CFLAGS to C compiler and linker:
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")

add_executable(tomp tomp.c)
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(omp C)
find_package(OpenMP REQUIRED)
# OPENMP_FOUND

# Not OK
# pass OpenMP flag to C compiler (only)
set_property(SOURCE tomp.c
  PROPERTY COMPILE_FLAGS ${OpenMP_C_FLAGS}
  )

# OK
# pass OpenMP CFLAGS to C compiler and linker:
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")

add_executable(tomp tomp.c)

$ cat tomp.c
#include <math.h>

int main()
{
  const int size = 256;
  double table[size];

  int n;
#pragma omp parallel for
  for(n=0; n<size; ++n)
    table[n] = 0;

  return table[size-1];
}

$ cmake . && make
[...]
Linking C executable tomp
CMakeFiles/tomp.dir/tomp.c.o: In function `main':
tomp.c:(.text+0x87): undefined reference to `GOMP_parallel_start'
tomp.c:(.text+0x98): undefined reference to `GOMP_parallel_end'
CMakeFiles/tomp.dir/tomp.c.o: In function `main._omp_fn.0':
tomp.c:(.text+0xe3): undefined reference to `omp_get_num_threads'
tomp.c:(.text+0xeb): undefined reference to `omp_get_thread_num'
collect2: error: ld returned 1 exit status
make[2]: *** [tomp] Error 1
make[1]: *** [CMakeFiles/tomp.dir/all] Error 2
make: *** [all] Error 2
No tags attached.
related to 0015393closed Kitware Robot undefined reference to `GOMP_parallel' 
Issue History
2015-02-09 10:58Mathieu MalaterreNew Issue
2015-02-10 09:01Brad KingRelationship addedrelated to 0015393
2016-06-10 14:29Kitware RobotNote Added: 0042709
2016-06-10 14:29Kitware RobotStatusnew => resolved
2016-06-10 14:29Kitware RobotResolutionopen => moved
2016-06-10 14:29Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0042709)
Kitware Robot   
2016-06-10 14:29   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.