How to compile an MPI included c program using cmake

前端 未结 2 1564
傲寒
傲寒 2020-11-28 14:34

I am trying to apply openmp and mpi techniques to an open source C program which requires \"cmake . && make\" to be built. I already found at How to set linker flags

相关标签:
2条回答
  • 2020-11-28 15:16

    In modern CMake 3.X which is target based, the CMakeLists.txt should look like this:

    cmake_minimum_required(VERSION 3.0)
    
    project(main)
    
    find_package(MPI REQUIRED)
    # add this line only when you are using openmpi which has a different c++ bindings
    add_definitions(-DOMPI_SKIP_MPICXX)
    
    # Use imported targets would make things much eazier. Thanks Levi for pointing it out.
    add_executable(main main.cpp)
    target_link_libraries(main
      PRIVATE
      MPI_C)
    
    # Old way.
    #target_link_libraries(main
    # PRIVATE
    # ${MPI_C_LIBRARIES})
    
    #target_include_directories(main
    # PRIVATE
    # ${MPI_C_INCLUDE_PATH}) 
    
    0 讨论(0)
  • 2020-11-28 15:29

    OpenMP

    Is this a question about OpenMP? Then all you have to do is compile with -fopenmp which you can do by appending it to CMAKE_C_FLAGS, for example:

    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp)
    

    MPI

    For MPI, you have to find mpi first

    find_package(MPI) #make it REQUIRED, if you want
    

    then add it's header files to your search path

    include_directories(SYSTEM ${MPI_INCLUDE_PATH})
    

    and finally link your program(s) (which is my_mpi_target in my case)

     target_link_libraries(my_mpi_target ${MPI_C_LIBRARIES})
    
    0 讨论(0)
提交回复
热议问题