基于CMake: set path to MPI headers and binaries manually,我将cmake文件设置为:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(TARGET "test")
PROJECT(${TARGET})
add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS "-std=c++11" CACHE STRING "compile flags" FORCE)
find_package(MPI REQUIRED)
if (MPI_FOUND)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
else (MPI_FOUND)
message(SEND_ERROR "couldn't find MPI")
endif (MPI_FOUND)
add_executable(${TARGET} somefiles)
target_link_libraries(${TARGET} ${MPI_LIBRARIES})
,效果很好。但是当我用cmake3这样做时:
CMAKE_MINIMUM_REQUIRED(VERSION 3.13.4)
the rest is the same as above
我收到此错误:
CMake Error at /home/nourisaj/c++/WP1/build/CMakeFiles/CMakeTmp/cmTC_3d1ffTargets.cmake:16 (add_library):
Target "MPI::MPI_C" links to item " -Wl,-rpath -Wl,/usr/lib64/openmpi/lib
-Wl,--enable-new-dtags" which has leading or trailing whitespace. This is
now an error according to policy CMP0004.
Call Stack (most recent call first):
/home/nourisaj/c++/WP1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:14 (include)
CMake Error at /home/nourisaj/c++/WP1/build/CMakeFiles/CMakeTmp/cmTC_3d1ffTargets.cmake:16 (add_library):
Target "MPI::MPI_C" links to item " -Wl,-rpath -Wl,/usr/lib64/openmpi/lib
-Wl,--enable-new-dtags" which has leading or trailing whitespace. This is
now an error according to policy CMP0004.
Call Stack (most recent call first):
/home/nourisaj/c++/WP1/build/CMakeFiles/CMakeTmp/CMakeLists.txt:14 (include)
CMake Error at /usr/share/cmake3/Modules/FindMPI.cmake:1187 (try_compile):
Failed to generate test project build system.
Call Stack (most recent call first):
/usr/share/cmake3/Modules/FindMPI.cmake:1228 (_MPI_try_staged_settings)
/usr/share/cmake3/Modules/FindMPI.cmake:1488 (_MPI_check_lang_works)
CMakeLists.txt:19 (find_package)
有什么主意吗?
答案 0 :(得分:1)
您遇到CMP0004
策略错误。在早期版本的CMake中,链接库会自动删除任何前导或尾随空格。在某些情况下,这可能导致微妙的,不希望的错误,因此CMake现在强制执行此策略,NEW
行为要求链接库的前导或尾随空格为 no 。看来您的MPI目标之一与此链接在一起,它具有领先的空白字符:
" -Wl,-rpath -Wl,/usr/lib64/openmpi/lib -Wl,--enable-new-dtags"
基于此CMake issue,此策略错误似乎已从CMake 3.9开始修复。如果您运行的CMake早于3.9,则必须自行修改FindMPI.cmake
文件以删除前导空白,或者您可以告诉CMake使用旧行为执行此策略(静默删除前导空白) ):
cmake_policy(SET CMP0004 OLD)
以下是cmake_policy
的文档。虽然可以在FindMPI.cmake
文件中设置此策略,但不建议这样做。部署CMake策略是为了避免OLD
的行为,因此建议您从文件中手动删除空格。
注意:与使用不同版本的CMake进行测试时一样,请确保在删除版本时要删除CMake缓存并从头开始构建。