2016-02-22 44 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()