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

问题描述 投票:-2回答:1

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

我试图找到/开发一个非常简单的例子,说明如何在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!!\n");
   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

感谢您提出的任何建议。

c++ visual-studio cmake compiler-errors mpi
1个回答
0
投票

我能够通过使用以下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\rhk12\code\cmake-visualstudio-msmpi\build\src\Debug\hellocmake.exe (process 18728) exited with code 0.
Press any key to close this window . . .

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

© www.soinside.com 2019 - 2024. All rights reserved.