[CMake] How to compile MPI code

Michael Wild themiwi at gmail.com
Wed Jun 22 08:40:16 EDT 2011


On 06/22/2011 12:00 PM, Mª Dolores Villalobos Ortiz wrote:
> Hi
> 
> I'm trying to compile a main program (it contains MPI functions) using
> cmake. I get an executable file (there aren't problems in compilation ),
> but it doesn't run correctly.
> 
> If I compile my code using a simple Makefile, it works; but I need to
> compile it using cmake (that's a prerequisite for my work).
> 
> I have installed MPICH2 and cmake 2.8.4. and I use the next files:
> 
> CMakeLists.txt:
> 
> cmake_minimum_required(VERSION 2.7)
> set(CMAKE_CXX_COMPILER "/home/lola/Packages/mpich2-1.3.2p1/bin/mpicxx")
> set(CMAKE_C_COMPILER "/home/lola/Packages/mpich2-1.3.2p1/bin/mpicc")
> project( ITK_MPI )
> find_package ( MPI )
> if ( MPI_FOUND )
>         include_directories( ${MPI_INCLUDE_PATH} )
> endif( MPI_FOUND )
> 

This is completely, utterly wrong!


# 2.7 was a development version, so you should either specify
# 2.6 or 2.8...
cmake_minim_required(VERSION 2.7)
project(ITK_MPI)

find_package(MPI REQUIRED)

include_directories(${MPI_INCLUDE_PATH})

add_executable(hello hello.cpp)
target_link_libraries(hello ${MPI_LIBRARIES})

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

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


You should *NEVER* set CMAKE_<LANG>_COMPILER, this completely breaks
things. Also, with CMake you shouldn't use the compiler wrappers (mpicxx
etc.), because CMake figures out how to compile MPI programs without it.

HTH

Michael


More information about the CMake mailing list