2016-02-22 19 views
2

我有以下CMakeLists:CMake的连接之前指定源

cmake_minimum_required(VERSION 3.3) 
project(untitled) 

set(SOURCE_FILES main.cpp) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/kernael/.openmpi/include -pthread -Wl,-rpath -Wl,/home/kernael/.openmpi/lib -Wl,--enable-new-dtags -L/home/kernael/.openmpi/lib -lmpi_cxx -lmpi") 

add_executable(untitled ${SOURCE_FILES}) 

但构建似乎失败,因为CMake的自动设定后的“-l”选项中源(main.cpp中),这似乎是的问题,因为命令行下面的命令工作:

g++ -I/home/kernael/.openmpi/include -pthread -L/home/kernael/.openmpi/lib main.cpp -lmpi_cxx -lmpi 

但是这一次不和产生相同的错误,CMake的构建:

g++ -I/home/kernael/.openmpi/include -pthread -L/home/kernael/.openmpi/lib -lmpi_cxx -lmpi main.cpp 

如何告诉CMake在链接发生之​​前指定源文件?

回答

1

不能使用CMAKE_CXX_FLAGS在CMake的包括,它只适用于编译器选项。

你必须找到与find_package MPI。 CMake然后找到包含路径和库。

find_package(MPI) 
if (MPI_C_FOUND) 
    include_directories(${MPI_INCLUDE_PATH}) 
    add_executable(untitled ${SOURCE_FILES}) 
    target_link_libraries(untitled ${MPI_LIBRARIES}) 
    set_target_properties(untitled PROPERTIES 
         COMPILE_FLAGS "${MPI_COMPILE_FLAGS}") 
else() 
    # MPI not found ... 
endif()