在Windows CMD中编译并运行C++代码。

huangapple go评论77阅读模式
英文:

Compile and run C++ codes in Windows CMD

问题

我安装了MS-MPI
我可以在Windows命令行中运行Python MPI代码。
我喜欢在Sublime Text中编写代码,尤其是C/C++。
我在CMD中使用gcc/g++运行我的代码。

> 我如何在命令行中编译和运行MPI代码?

我不喜欢使用Visual Studio。

那么,是否有办法可以在命令行中使用MS-MPI编译和运行我的C/C++ MPI代码?

我知道标题中有两个问题:

> 在Windows上编译C++ MPI代码。

但没有明确的答案或评论。这就是为什么我现在重新提问的原因。

我找不到一个适用于Windows命令行的教程,大多数都是使用Visual Studio。

英文:

I installed MS-MPI.
I can run Python MPI codes in my Windows-CMD.
I like to code in sublime text, especially C/C++.
I run my code in the CMD using gcc/g++.

> How can I compile and run MPI-codes from the command line?

I don't like using Visual Studio.

So, is there any way I can compile and run my C/C++-MPI codes with MS-MPI in the command line?

I know there are two questions in the title :

> Compile C++ MPI Code on Windows.

But there's no clear answer or comment. That's why I am re-asking now.

I couldn't find a single tutorial that was for the Windows command line, mostly it was with Visual Studio.

答案1

得分: 0

假设您的路径中已经安装了g++和MS-MPI例程,那么在编译程序时,您只需设置包含文件和MPI库文件的位置。

最简单的方法是使用批处理文件。将以下内容复制到您打算编译代码的目录(或路径中的任何其他位置),并将其命名为mp.bat

set OPT1="C:\Program Files (x86)\Microsoft SDKs\MPI\Include"
set OPT2="C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpi.lib"
g++ -I%OPT1% -o %1.exe %1.cpp %OPT2%

如果您的程序命名为sendAndRecv.cpp,那么您可以使用批处理文件来编译并链接它,创建sendAndRecv.exe,只需执行以下命令:

mp sendAndRecv

然后,您可以运行它(例如,在2个处理器上运行):

mpiexec -n 2 sendAndRecv.exe

在不使用集成开发环境(IDE)时,使用批处理文件可以使生活更轻松。

英文:

Assuming that you have g++ and the MS-MPI routines in your path then you simply need to set the location of include files and MPI library files when you run g++ to compile your program.

The easiest way to do this is with a batch file. Copy the following to the directory where you are going to compile your code (or anywhere else in your path) and call it mp.bat

set OPT1="C:\Program Files (x86)\Microsoft SDKs\MPI\Include"
set OPT2="C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64\msmpi.lib"
g++ -I%OPT1% -o %1.exe %1.cpp %OPT2%

If your program is called sendAndRecv.cpp then you can use your batch file to compile and link it to create sendAndRecv.exe by just

mp sendAndRecv

Then you run it (on, say, 2 processors) with, e.g.

mpiexec -n 2 sendAndRecv.exe

Use batch files to make life easy when you aren't using an IDE.

huangapple
  • 本文由 发表于 2023年5月26日 15:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定