[CMake] Specify an alternate C++ compiler for some source files

Michael Wild themiwi at gmail.com
Wed Mar 30 08:10:54 EDT 2011


On 03/30/2011 01:42 PM, Marc Tajchman wrote:
> Hi,
> 
> How to specify different compilers for different source files in
> CMakeLists.txt, e.g. if
> 
> test1.cxx must be compiled with g++
> test2.cxx must be compiled with mpicxx (mpi compiler)
> 
> I tried
> 
>> add_executable(test1.exe test1.cxx)
>> set_source_files_properties(test2.cxx PROPERTIES CMAKE_CXX_COMPILER
> ${MPI_COMPILER})
>> add_executable(test2.exe test2.cxx)
> 
> but it didn't work (i.e. g++ is still used for test2.cxx -> test2.o).
> 
> Any help appreciated.
> 
> Best Regards,
> Marc
>

CMake can't do that. But if you need MPI, use

find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
add_executable(test1 test1.cxx)
add_executable(test2 test2.cxx)
target_link_libraries(test2 ${MPI_LIBRARIES})
set_target_properties(test2 PROPERTIES
  COMPILE_FLAGS "${MPI_COMPILE_FLAGS}"
  LINK_FLAGS "${MPI_LINK_FLAGS}")


HTH

Michael





More information about the CMake mailing list