Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking several programs with CMake

Tags:

c

linker

cmake

I have three programs which computes a quantity S in serial code, and in parallel using MPI and OpenMP. All programs needs the same header file utils.h. Somehow, after running cmake .., I need to run make twice to compile the program correctly as the first run results in linking errors (the standard libraries seems not to be linked). Any ideas why this is the case?

cmake_minimum_required(VERSION 2.6)
project(compute_S)

find_package(MPI)
enable_language(C)

include_directories(${MPI_INCLUDE_PATH})

set(CMAKE_C_FLAGS "-std=c99")
add_executable(compute_S compute_S.c utils.h)

set(CMAKE_C_FLAGS "-std=c99")
add_executable(compute_S_MPI compute_S_MPI.c utils.h)

set(CMAKE_C_FLAGS "-std=c99 -fopenmp")
add_executable(compute_S_OpenMP compute_S_OpenMP.c utils.h)

target_link_libraries(compute_S ${catkin_LIBRARIES})
target_link_libraries(compute_S_MPI ${MPI_C_LIBRARIES})
target_link_libraries(compute_S_OpenMP ${catkin_LIBRARIES})

You can reproduce the error by running the following

mkdir test 
cd test 
git clone https://github.com/Zetison/exerciseSet4 
cd exerciseSet4 
cmake . 
make
like image 553
user2978680 Avatar asked Apr 29 '26 01:04

user2978680


1 Answers

I tried the example at https://cmake.org/pipermail/cmake/2011-June/045037.html which shows how to link MPI. The module FindMPI.cmake creates the following variables:

  • MPI_INCLUDE_PATH : the include search path
  • MPI_LIBRARIES: the name of the mi library.
  • MPI_COMPILE_FLAGS and MPI_LINK_FLAGS: flags to compile and link.

Similarly, the FindOpenMP.cmake module creates the variables OpenMP_C_FLAGS, OpenMP_Fortran_FLAGS and OpenMP_CXX_FLAGS which correspond to MPI_LINK_FLAGS. Indeed, it can resume to -fopenmp.

Could you try the following CMakeLists.txt by typing cmake . then make ?

cmake_minimum_required(VERSION 2.6)
project(compute_S)

find_package(MPI REQUIRED)
find_package(OpenMP REQUIRED)
enable_language(C)

include_directories(${MPI_INCLUDE_PATH})

set(CMAKE_C_FLAGS "-std=c99")
add_executable(compute_S compute_S.c utils.h)

set(CMAKE_C_FLAGS "-std=c99")
add_executable(compute_S_MPI compute_S_MPI.c utils.h)
# https://cmake.org/pipermail/cmake/2011-June/045037.html
target_link_libraries(compute_S_MPI ${MPI_LIBRARIES})

if(MPI_COMPILE_FLAGS)
  set_target_properties(compute_S_MPI PROPERTIES
    COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
endif()

if(MPI_LINK_FLAGS)
  set_target_properties(compute_S_MPI PROPERTIES
    LINK_FLAGS "${MPI_LINK_FLAGS}")
endif()

set(CMAKE_C_FLAGS "-std=c99")
add_executable(compute_S_OpenMP compute_S_OpenMP.c utils.h)

# https://cmake.org/cmake/help/v3.0/module/FindOpenMP.html
#message(${OpenMP_C_FLAGS})
if(OpenMP_C_FLAGS)
  set_target_properties(compute_S_OpenMP PROPERTIES
    LINK_FLAGS "${OpenMP_C_FLAGS}")
endif()

target_link_libraries(compute_S ${catkin_LIBRARIES})
target_link_libraries(compute_S_MPI ${MPI_C_LIBRARIES})
target_link_libraries(compute_S_OpenMP ${catkin_LIBRARIES})
like image 88
francis Avatar answered Apr 30 '26 18:04

francis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!