使用CMake,Microsoft MPI和Visual Studio 2017 - 找不到mpi.h [重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用CMake,Microsoft MPI和Visual Studio 2017 - 找不到mpi.h [重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我试图找到/开发一个非常简单的例子,说明如何在Visual Studio中使用CMake和Microsoft MPI。我让CMake运作良好,并且通常了解如何创建CMake项目。我也有MS MPI与Visual Studio合作并确认我可以按照这个例子创建一个MPI项目:https://blogs.technet.microsoft.com/windowshpc/2015/02/02/how-to-compile-and-run-a-simple-ms-mpi-program/

我只是无法让他们一起工作。具体来说,我正在寻找CMake命令放入我的CMakeLists.txt文件,该文件在Visual Studio 17中启用MS MPI构建。我创建了一个CMake项目(在这里找到:https://github.com/PSUCompBio/cmake-visualstudio-msmpi)编译并运行良好,不包括:

#include "mpi.h"

我用来构建项目的步骤是:1。使用Cmake-GUI应用程序配置和生成makefile 2.使用CMake-GUI中的打开项目按钮启动Visual Studio 2017. 3.将HelloWorld解决方案设置为启动项目。 4.构建(到顶部菜单栏,然后​​构建 - >构建解决方案)

但是,包括mpi.h:

#include <stdio.h>

#include "mpi.h"

int main()
{
   // printf() displays the string inside quotation
   printf("Hello, World!!
");
   return 0;
}

找不到mpi.h: visual studio error from build

我的顶级CmakeLists.txt文件很简单,确实发现MSMPI很好:

cmake_minimum_required(VERSION 3.0)
PROJECT (hellocmake)
ADD_SUBDIRECTORY (src)
option(ENABLE_MPI "Enable MPI parallelization" OFF)
if(ENABLE_MPI)
    if(WIN32)
        find_package(MPI REQUIRED)
        if(MPI_FOUND)
            message("Using MPI")
        endif(MPI_FOUND)
    endif(WIN32)
endif(ENABLE_MPI)

我的项目结构是:

-CMakeLists.txt
-src
----test.cpp
----CMakeLists.txt

我的主要问题是为什么没有找到mpi.h?我可以看到MS MPI包含目录由CMake-GUI定位(见下图):CMake-GUI configure showing MS MPI include path

我需要在我的顶级CMakeLists.txt文件中放置另一个CMake命令吗?

下面我展示了处理MPI选项的CMakeCache.txt的一部分:

//Enable MPI parallelization
ENABLE_MPI:BOOL=ON

//Executable for running MPI programs.
MPIEXEC_EXECUTABLE:FILEPATH=C:/Program Files/Microsoft MPI/Bin/mpiexec.exe

//Maximum number of processors available to run MPI applications.
MPIEXEC_MAX_NUMPROCS:STRING=2

//Flag used by MPI to specify the number of processes for mpiexec;
// the next option will be the number of processes.
MPIEXEC_NUMPROC_FLAG:STRING=-n

//These flags will be placed after all flags passed to mpiexec.
MPIEXEC_POSTFLAGS:STRING=

//These flags will be directly before the executable that is being
// run by mpiexec.
MPIEXEC_PREFLAGS:STRING=

//MPI CXX additional include directories
MPI_CXX_ADDITIONAL_INCLUDE_DIRS:STRING=

//MPI compiler for CXX
MPI_CXX_COMPILER:FILEPATH=MPI_CXX_COMPILER-NOTFOUND

//MPI CXX compilation definitions
MPI_CXX_COMPILE_DEFINITIONS:STRING=

//MPI CXX compilation flags
MPI_CXX_COMPILE_OPTIONS:STRING=

//Path to a file.
MPI_CXX_HEADER_DIR:PATH=C:/Program Files (x86)/Microsoft SDKs/MPI/Include

//MPI CXX libraries to link against
MPI_CXX_LIB_NAMES:STRING=msmpi

//MPI CXX linker flags
MPI_CXX_LINK_FLAGS:STRING=

//If true, the MPI-2 C++ bindings are disabled using definitions.
MPI_CXX_SKIP_MPICXX:BOOL=OFF

//MPI C additional include directories
MPI_C_ADDITIONAL_INCLUDE_DIRS:STRING=

//MPI compiler for C
MPI_C_COMPILER:FILEPATH=MPI_C_COMPILER-NOTFOUND

//MPI C compilation definitions
MPI_C_COMPILE_DEFINITIONS:STRING=

//MPI C compilation flags
MPI_C_COMPILE_OPTIONS:STRING=

//Path to a file.
MPI_C_HEADER_DIR:PATH=C:/Program Files (x86)/Microsoft SDKs/MPI/Include

//MPI C libraries to link against
MPI_C_LIB_NAMES:STRING=msmpi

//MPI C linker flags
MPI_C_LINK_FLAGS:STRING=

//Location of the msmpi library for Microsoft MPI
MPI_msmpi_LIBRARY:FILEPATH=C:/Program Files (x86)/Microsoft SDKs/MPI/Lib/x86/msmpi.lib

//Value Computed by CMake
hellocmake_BINARY_DIR:STATIC=C:/Users/rhk12/code/cmake-visualstudio-msmpi/build

//Value Computed by CMake
hellocmake_SOURCE_DIR:STATIC=C:/Users/rhk12/code/cmake-visualstudio-msmpi

感谢您提出的任何建议。

答案

我能够通过使用以下CMakeLists.txt来实现这一点。

在顶级目录中,CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

PROJECT (hellocmake)

option(ENABLE_MPI "Enable MPI parallelization" OFF)

if(ENABLE_MPI)
    find_package(MPI REQUIRED)
    include_directories(${MPI_INCLUDE_PATH})
    set(CMAKE_C_FLAGS "${CMAKE_FLAGS} ${MPI_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_EXE_LINKER_FLAGS}")
endif(ENABLE_MPI)

ADD_SUBDIRECTORY (src)

然后在src目录下CMakeLists.txt

ADD_EXECUTABLE(hellocmake test.cpp)

if(ENABLE_MPI)
    target_link_libraries(hellocmake ${MPI_LIBRARIES})
endif(ENABLE_MPI)

一旦我这样做,我回到CMake-GUI并配置并生成Makefile,然后选择打开Visual Studio的打开项目。当我构建并运行时,我得到了这个:

Hello, World!!
Hello world from processor MNE-REKR02.engr.psu.edu, rank 0 out of 1 processors

C:Users
hk12codecmake-visualstudio-msmpiuildsrcDebughellocmake.exe (process 18728) exited with code 0.
Press any key to close this window . . .

我不确定如何指定visual studio中的处理器数量。 CMake配置找到2,但这是一个不同的问题。

以上是关于使用CMake,Microsoft MPI和Visual Studio 2017 - 找不到mpi.h [重复]的主要内容,如果未能解决你的问题,请参考以下文章

cmake编译单/多文件

需要 Microsoft Visual C++ 14.0。使用“Microsoft Visual C++ 构建工具”获取它:http://landinghub.visualstudio.com/vis

使用 CMake 编译后无法从 mpich2 使用 mpirun

MPI:更改 CMakelists 中的处理器数量

在 Visual Studio 2017 中使用 Cmake 构建 ssh.dll

mpi_get_processor_name 函数中的“TBD”一词是啥意思