【问题标题】:MPI only compiles in Visual Studio debug sectionMPI 仅在 Visual Studio 调试部分编译
【发布时间】:2021-11-14 08:29:26
【问题描述】:

我有这个程序

#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int size;
    int rank;
    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    printf("Hello, World!");
    cout << MPI_Comm_rank << " " << MPI_Comm_size << endl;
    MPI_Finalize();
    return 0;
}

当我在 Microsoft Visual Studios 中调试它时,它工作得很好。但是当我在命令提示符或任何终端中尝试时,我得到了这个错误,

Project_1.cpp:2:10: fatal error: mpi.h: No such file or directory
    2 | #include <mpi.h>
      |          ^~~~~~~
compilation terminated.

我已经安装了 g++,它可以与其他程序一起正常工作。我也安装了 MPI。能够将它链接到我的 Microsoft Visual Studio,但它只适用于其调试部分。我正在使用Windows计算机。不太确定该怎么做。任何帮助都会非常感谢。

【问题讨论】:

  • 您已将包含路径添加到 Microsoft Visual Studio 的调试设置中 - 您需要在发布配置和任何命令提示符会话中使用相同的信息。
  • 您的cout 语句包含两个函数名称。我很惊讶这会起作用,而且它肯定不会给出您期望的输出。
  • @VictorEijkhout:函数的名称将计算为函数的地址。猜测一下,它会被转换为void * 以打印出来。

标签: c++ windows terminal compiler-errors mpi


【解决方案1】:

你的程序有问题:

  • 您没有定义MPI_Comm_rankMPI_Comm_size,但您尝试在cout 中打印它们。在下面的代码中,我重写了您的代码。
#include <stdio.h>
#include <mpi.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int size;
    int rank;
    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    printf("Hello, World!");
    cout << rank << " " << size << endl;
    MPI_Finalize();
    return 0;
}
  • 您还需要通过以下方式将mpiexec.exe 的位置添加到您的系统PATH 中:

    1- 打开Window’s System Properties 对话框 => 点击Environment Variables

    2- 点击edit 获取路径变量 => 添加C: Program Files\Microsoft MPI\Bin \(假设默认安装)

  • 包含适当的库。

    1- MPI 代码应编译为控制台应用程序

    2- 创建项目后,您需要添加适当的库

    • 右击Solution Explorer中的项目,进入Properties
    • 点击VC++ Directories并(假设MPI SDK安装在默认目录中)添加:
      • C:/ Program Files (x86)/Microsoft SDKs/Include 包含目录
      • C:/Program Files (x86)/Microsoft SDKs/Lib/x64C:/Program Files (x86)/Microsoft SDKs/Lib/x86 到图书馆目录
      • 取决于您希望将代码编译为 32 位还是 64 位

    3- 点击链接器 =>输入

    • 在附加依赖项下添加msmpi.lib

【讨论】:

  • 执行此操作时出现完全相同的错误。由于某种原因,它仍然无法识别终端上的 ,但会在调试器上识别。我似乎也无法在我的计算机上的任何地方找到 mpiexec.exe 文件。是否有可能它没有与 MPI 的其余部分一起下载?由于某种原因,我遇到了很多麻烦。
  • 我建议你重新下载。可能你没有正确下载。您需要安装 Microsoft MPI: • docs.microsoft.com/en-us/message-passing-interface/… • 单击下载并下载并安装“msmpisdk.msi”和“msmpisetup”。
  • 只是这样做了,同样的问题出现了。程序文件中似乎也没有 Microsoft MPI 文件夹,但 Microsoft SDK 位于程序文件 (x86) 中。
  • Microsoft MPI 应位于 program files 中,Microsoft SDK 应位于 Program files (x86) 中。
猜你喜欢
  • 2017-02-13
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多