# 如何在 CLion 中处理 OpenMPI 项目

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

[OpenMPI](https://www.open-mpi.org) 是 [消息传递接口 (MPI)](https://www.mpi-forum.org) 库的一个开源实现，该标准旨在支持高性能并行计算。 OpenMPI 结合了多个 MPI 项目的技术和资源。

本教程将指导您在 CLion 中完成 OpenMPI 项目的配置、编译、运行和调试。 这些说明是通用的，您可以将它们应用到您的源代码中。 为了说明这些步骤，我们使用了一个包含 OpenMPI 函数的简单 C++ 应用程序。 这些说明是针对在 macOS 上工作的情况给出的。

Procedure: 0. 开始之前：在您的系统上安装 OpenMPI

1. [下载](https://www.open-mpi.org/software/ompi)并 [构建](https://www.open-mpi.org/faq/?category=building#easy-build) OpenMPI。 如果出现错误，请尝试官方常见问题页面上提供的故障排除提示。

> **Tip:**
> 提示  ：如果  `make all install`  失败并显示  权限被拒绝 ，请尝试使用  `sudo`  运行它。

2. 作为一个简单的检查，从命令行运行  `mpicc`  - 您的系统应该能够识别该命令。

Procedure: 1. 在 CLion 中创建一个新项目

1. 从主菜单中选择  `文件 | 新建 | 项目...`  或在欢迎屏幕上点击   新项目  。

2. ![启动一个新的 C++ 项目](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_startproject.png)

在左侧窗格中，选择项目类型（在我们的示例中为   C++ 可执行文件  ）。

提供项目位置并设置语言标准。

点击   创建  。

3. CLion 将生成一个 Stub CMake 项目：

![Stub CMake 项目](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_stubproject.png)

Procedure: 2. 添加您的源文件

1. 将您的源文件复制到项目文件夹。

在我们的示例中，文件是  `openmpi_test.cpp`  和  `openmpi_test.h` 。 如果您想将它们用作示例，请复制粘贴以下代码，或者使用您自己的文件：

```CPLUSPLUS
#include <mpi.h>
#include <cstdio>

void print_processors() {

    // Initialize the MPI environment
    MPI_Init(nullptr, nullptr);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    // Print off a hello world message
    printf("Hello world from processor %s, rank %d out of %d processors\n",
    processor_name, world_rank, world_size);

    // Finalize the MPI environment.
    MPI_Finalize();
}

```

```CPLUSPLUS
#ifndef OPENMPI_TEST_OPENPMI_HELLO_H
#define OPENMPI_TEST_OPENPMI_HELLO_H

void print_processors();

#endif //OPENMPI_TEST_OPENPMI_HELLO_H

```

```CPLUSPLUS
#include "openmpi_test.h"

int main() {
    print_processors();
    return 0;
}

```

此时，新文件尚未添加到 CMake 项目结构中，OpenMPI 代码无法被 IDE 识别：

![在添加到 CMakeLists.txt 之前，OpenMPI 文件无法被识别](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_notrecognized_initially.png)

2. 将文件添加到 CMake。

* 要添加单个文件，请点击通知栏中的 添加到 CMake 项目 。

* 要添加多个文件，请在项目树中选择它们，并从上下文菜单中调用 添加到 CMake 项目 。

选择目标，CLion 将自动添加文件：

![将文件添加到 CMake 目标](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_addtotarget.png)

Procedure: 3. 调整 CMakeLists.txt 并加载项目

打开  `CMakeLists.txt`  文件并进行以下更改：

1. 在  `add_executable`  之前添加此行：

```CMAKE
find_package(MPI REQUIRED)

```

2. 在  `add_executable`  之后添加此行：

```CMAKE
target_link_libraries(<your_project_name> PRIVATE MPI::MPI_CXX)

```

`CMakeLists.txt`  内容示例：

```CMAKE
cmake_minimum_required(VERSION 3.25)
project(openmpi_test)

set(CMAKE_CXX_STANDARD 17)

# Add MPI Package to Project
find_package(MPI REQUIRED)

add_executable(openmpi_test main.cpp openmpi_hello.cpp)

# Link MPI libraries
target_link_libraries(openmpi_test PRIVATE MPI::MPI_CXX)

```

3. 当您在  `CMakeLists.txt`  中进行更改时，CLion 会显示一个图标，提示重新加载项目结构。 点击它或按快捷键：

![重新加载 CMake](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_reloadchanges_icon.png)

CMake 重新加载和项目构建应成功完成：

![更改 CMakeLists.txt 脚本后构建 OpenMPI 项目](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openapi_build_success.png)

如果有错误，请仔细检查您的  `CMakeLists.txt`  和您的 OpenMPI 安装。

您可以在  `cmake-build-debug`  文件夹中找到二进制文件（或者在您更改的其他构建位置中）：

![构建目录中的二进制文件](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_binary.png)

Procedure: 4. 构建您的程序

使用以下选项之一：

* 从主菜单调用  `构建 | 构建项目` 。

有关此操作的更多信息，请参阅 [构建操作](build-actions.html)。

* CLion 会为  `CMakeLists.txt`  中检测到的每个目标自动创建一个类型为 [CMake Application](run-debug-configuration-makefile-application.html) 的配置。

要使用配置构建目标，请在切换器中选择它，然后点击锤子图标或按 `Ctrl+F9` (Windows), `⌘ F9` (macOS), `⌘ F9` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ B` (macOS System Shortcuts), `Ctrl+F9` (XWin), `Ctrl+F9` (GNOME), `Ctrl+9` (KDE), `Ctrl+F9` (Emacs), `Ctrl+F9` (Sublime Text), `Ctrl+F9` (Sublime Text (macOS)), `⌘ B` (Xcode), `F7` (Visual Studio), `F7` (Visual Studio (macOS)), `F7` (ReSharper), `F7` (ReSharper (macOS)), `Ctrl+F9` (QtCreator), `⌘ F9` (QtCreator (macOS)), `F11` (NetBeans), `Ctrl+F9` (Eclipse), `⌘ B` (Eclipse (macOS))：

![构建一个配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_build_config_toolbar.png)

> **Tip:**
> 您可以在   编辑配置   对话框中更改配置参数并创建更多配置。

Procedure: 5. （可选）配置 CMake 配置文件

在 [CMake 配置文件](cmake-profile.html) 设置中，您可以调整构建选项、环境变量、构建目录和其他参数。

1. 转到  `设置 | 构建、执行、部署 | CMake` 。

在此对话框中，您可以编辑配置文件设置并创建/删除配置文件。 有关更多信息，请参阅 [CMake 配置文件](cmake-profile.html)。

> **Tip:**
> 当您启动一个项目时，CLion 会创建一个   调试   配置文件。 如果您需要发布版本构建，请添加另一个类型为   Release   的配置文件。

![调试和发布配置文件](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_release_profile_added.png)

2. 项目配置文件列在配置切换器中。 在构建 CMake 配置之前，请确保选择所需的配置文件。

![配置切换器中的配置文件](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_profiles_switcher.png)

Procedure: 5. 运行应用程序

OpenMPI 程序应在特殊的启动器下运行，  `mpiexec` 、  `mpirun`  或  `orterun` （用于在 OpenMPI 中执行串行和并行作业的同义词）。 要使用所需的程序参数调用它们，请使用 CLion 的   Shell 脚本   配置。

1. 在主菜单中，进入  `运行 | 编辑配置` .

2. 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg) 并选择   Shell 脚本  ：

![添加一个 Shell Script 配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_add_shellscript.png)

3. 调整配置设置：

* 编辑配置名称。

* 在 执行： 中，选择 脚本文本 。

* 在 脚本文本 中，指定运行程序的命令。 在我们的示例中，它是 `mpiexec -n 4 ./cmake-build-debug/openmpi_test`

![Shell Script 配置设置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_shellscript_config_settings.png)

4. 保存设置，然后在切换器中选择新创建的配置并点击 ![运行 按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.execute.svg) 或按 `Shift+F10` (Windows), `⌃ R` (macOS), `⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ R` (macOS System Shortcuts), `Shift+F10` (XWin), `Shift+F10` (GNOME), `Shift+F10` (KDE), `Shift+F10` (Emacs), `Shift+F10` (Sublime Text), `Shift+F10` (Sublime Text (macOS)), `⌘ ⌥ ⇧ R` (Xcode), `Ctrl+F5` (Visual Studio), `⌃ F5` (Visual Studio (macOS)), `Ctrl+F5` (ReSharper), `⌃ F5` (ReSharper (macOS)), `Ctrl+R` (QtCreator), `⌘ R` (QtCreator (macOS)), `F6` (NetBeans), `Alt+Shift+X` (Eclipse), `⌘ ⇧ F11` (Eclipse (macOS))。

![运行 Shell Script 配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_runshellscript_toolbar.png)

您的程序应成功启动。 CLion 将在   终端   工具窗口中显示输出：

![控制台中的 OpenMPI 程序输出](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_run_output.png)

Procedure: 6. 通过附加到进程进行调试

为了在 CLion 中调试 OpenMPI 应用程序，您需要先启动它，然后将调试器附加到每个正在运行的进程。

1. 通过点击所需行左侧的边距，在代码中放置 [断点](using-breakpoints.html)。

2. 在每个断点之前添加以下代码片段：

```CPLUSPLUS
int i = 0;
while (!i)
    sleep(5);

```

3. 在  `sleep(5);`  行上放置一个断点。

![代码中的断点用于调试](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_code_beforedebug.png)

4. 使用  `构建 | 构建项目`  或 [使用配置](#build-profile-config) 重新构建。

确保选择了   调试   配置文件。

5. 运行 Shell Script 配置。 在切换器中选择它，然后点击 ![运行 按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.actions.execute.svg) 或按 `Shift+F10` (Windows), `⌃ R` (macOS), `⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ R` (macOS System Shortcuts), `Shift+F10` (XWin), `Shift+F10` (GNOME), `Shift+F10` (KDE), `Shift+F10` (Emacs), `Shift+F10` (Sublime Text), `Shift+F10` (Sublime Text (macOS)), `⌘ ⌥ ⇧ R` (Xcode), `Ctrl+F5` (Visual Studio), `⌃ F5` (Visual Studio (macOS)), `Ctrl+F5` (ReSharper), `⌃ F5` (ReSharper (macOS)), `Ctrl+R` (QtCreator), `⌘ R` (QtCreator (macOS)), `F6` (NetBeans), `Alt+Shift+X` (Eclipse), `⌘ ⇧ F11` (Eclipse (macOS))。

![运行 Shell Script 配置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_runshellscript_toolbar.png)

6. 按 `Ctrl+Alt+F5` (Windows), `⌥ ⇧ F5` (macOS), `⌥ ⇧ F5` (IntelliJ IDEA Classic (macOS)), `⌥ ⇧ F5` (macOS System Shortcuts), `Ctrl+Alt+5` (XWin), `Ctrl+Alt+5` (GNOME), `Ctrl+Alt+5` (KDE), `Ctrl+Alt+F5` (Emacs), `Ctrl+Alt+F5` (Sublime Text), `⌥ ⇧ F5` (Sublime Text (macOS)), `⌥ ⇧ F5` (Xcode), `Ctrl+Alt+P` (Visual Studio), `⌘ ⌥ P` (Visual Studio (macOS)), `Ctrl+Alt+F5` (ReSharper), `⌃ ⌥ F5` (ReSharper (macOS)), `Ctrl+Alt+F5` (QtCreator), `⌥ ⇧ F5` (QtCreator (macOS)), `Ctrl+Alt+F5` (NetBeans), `Ctrl+Alt+F5` (Eclipse), `⌥ ⇧ F5` (Eclipse (macOS)) 或从主菜单中选择  `运行 | 附加到进程` 。

7. 在打开的对话框中，搜索您想要附加的进程并点击   附加方式...  。

![附加到进程](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_attach_dialog.png)

8. 在 [调试工具窗口](debug-tool-window.html) 的变量窗格中，选择  `i`  并按  `F2` 。 设置一个非零值：

![设置变量值](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_debug_setvalue.png)

9. 像往常一样继续调试。

附加调试器后，您可以使用所有 CLion 的调试功能，例如 [不同类型的断点](using-breakpoints.html#set-line-breakpoint)、 [单步操作](stepping-through-the-program.html)、 [内存](memory-view.html) 和 [反汇编](disassembly-view.html) 视图、 [表达式求值](examining-suspended-program.html#evaluating-expressions) 等。

![使用附加调试器逐步执行程序](https://resources.jetbrains.com/help/img/idea/2026.3/cl_openmpi_debug_stepping.png)

10. 重复步骤 6-9 以调试另一个进程。

## See also

### 外部链接

[https://www.open-mpi.org](https://www.open-mpi.org)

### 操作指南

[快速 CMake 教程](quick-cmake-tutorial.html) [Debugging](debugging-code.html) [附加到进程](attach-to-process.html)

### 概念

[CMake 配置文件](cmake-profile.html) [运行/调试配置](run-debug-configuration.html)

