首先,你需要几个东西:
- Homebrew
- brew安装gcc(只需要brew install gcc)
- VSCode官网
如果这些你都有,其实没有那么复杂
第一步:
新建文件夹VSCODEFORCPP
新建main.cpp文件
#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
printf("Hello, world. I am %d of %d\n", rank, nprocs);fflush(stdout);
MPI_Finalize();
//printf("hello!");
return 0;
}
第二步:
配置C++环境:
三个配置文件其实可以自动生成,详情请见MacOS安装vs code并且配置C/C++环境2020最新版
在.vscode文件夹中新建并修改:
- 文件c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
- 文件launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "输入程序名称,例如 ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
- 文件tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"${file}",
"-std=c++11",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-g",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
第三步:
mpicc main.cpp -o main.o
mpirun -n 4 main.o
得出结果: