You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改C++ Makefile以支持OpenMPI并行编译多文件生成单可执行文件

问题:修改Makefile支持OpenMPI并兼容Windows(Cygwin)与MacOS

本人对Makefile经验不足,若问题简单还请见谅!现有一个Makefile可将多个.cpp和.h文件编译为.o文件并生成可执行文件,当前使用g++编译器。希望修改该文件以支持OpenMPI多核计算,环境为Windows 11 x64架构(使用Cygwin构建运行代码),同时需兼容MacOS系统。注:并行化宏仅存在于项目主文件mag_spec_tracker.cpp中。

当前Makefile

IDIR = include
CXX = g++ 
CXXFLAGS = -I$(IDIR) -std=c++17
ODIR = obj
_DEPS = my_functions.h particle.h beam.h threevector.h threematrix.h screen.h magnet.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = my_functions.o particle.o beam.o mag_spec_tracker.o screen.o magnet.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

$(ODIR)/%.o: %.cpp $(DEPS)
    $(CXX) -c -o $@ $< $(CXXFLAGS)

run: $(OBJ)
    $(CXX) -o $@ $^ $(CXXFLAGS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(IDIR)/*~ 

尝试添加的MPI编译标志代码

MPI_COMPILE_FLAGS = $(shell mpicc --showme:compile)
MPI_LINK_FLAGS = $(shell mpicc --showme:link)

更新(2022/12/6)

根据Matzeri的建议(OpenMPI的C编译器应为mpicxx而非g),将Makefile中的g++替换为mpicxx后出现链接错误:

$ make
mpicxx -c -o obj/mag_spec_tracker.o mag_spec_tracker.cpp -Iinclude -std=c++17
mpicxx -o run obj/my_functions.o obj/particle.o obj/beam.o obj/mag_spec_tracker.o obj/screen.o obj/magnet.o -Iinclude -std=c++17
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmpi_cxx: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmpi: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopen-rte: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopen-pal: No such file or directory
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:19: run] Error 1

移除mingw64目录后,又出现编译器路径错误:

$ make
mpicxx -o run obj/my_functions.o obj/particle.o obj/beam.o obj/mag_spec_tracker.o obj/screen.o obj/magnet.o -Iinclude -std=c++17
--------------------------------------------------------------------------
The Open MPI wrapper compiler was unable to find the specified compiler
g++ in your PATH.

Note that this compiler was either specified at configure time or in
one of several possible environment variables.
--------------------------------------------------------------------------
make: *** [Makefile:19: run] Error 1

已将c:\cygwin\bin添加到环境变量,且确认mpicxx存在于该目录,但问题仍存在,求解决办法!


内容的提问来源于stack exchange,提问作者jsncrdrll

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:45:35