在Cygwin上使用MPI编译器构建Xbraid程序遇链接错误求助
Hey there! Let's figure out how to fix those linking errors you're seeing when building the ex-01-pp example with Xbraid in Cygwin. Those undefined references (like __gxx_personality_seh0, C++ RTTI vtables, and operator new) are a clear sign that the linker can't find the C++ standard library symbols it needs—here's how to fix this:
问题原因分析
Your compile command uses mpiCC (the MPI C++ compiler wrapper), but in Cygwin's environment, this wrapper might not automatically link the C++ standard library (libstdc++) during the final linking step. Since ex-01-pp.cpp is a C++ file, it relies on these standard library symbols for exceptions, RTTI, and memory management—missing them causes the linker errors you're seeing.
解决方案步骤
1. 确认依赖包已安装
First, double-check that you have the necessary Cygwin packages installed:
gcc-g++: Provides the C++ compiler and standard libraryopenmpi-devel: Provides MPI support for C++ (or the MPI package you're using)
You can verify this via the Cygwin setup tool if needed.
2. 修改Makefile的编译规则
The quickest fix is to explicitly link the C++ standard library in the ex-01-pp build rule. Edit your Makefile's ex-01-pp section to add -lstdc++ at the end of the link command:
ex-01-pp: ex-01-pp.cpp $(BRAID_LIB_FILE) @echo "Building" $@ "..." $(MPICXX) $(CXXFLAGS) $(BRAID_FLAGS) $(@).cpp -o $@ $(BRAID_LIB_FILE) $(LFLAGS) -lstdc++
Alternatively, you can add -lstdc++ to the global LFLAGS variable (either in your Makefile or makefile.inc) so it applies to all C++ targets:
LFLAGS += -lstdc++
3. 手动测试编译命令
If you want to test the fix before modifying the Makefile, run this command directly in your terminal:
mpic++ -g -Wall -I../braid ex-01-pp.cpp -o ex-01-pp ../braid/libbraid.a -lm -lstdc++
This explicitly calls the MPI C++ wrapper and links libstdc++—if this works, you know the Makefile modification is correct.
4. 检查MPI编译器配置
Ensure that the MPICXX variable in makefile.inc is pointing to the correct C++ MPI compiler wrapper. It should be set to either mpiCC or mpic++ (both are valid in Cygwin for OpenMPI).
为什么这能解决问题?
The -lstdc++ flag tells the linker to include the GNU C++ standard library, which provides all the missing symbols your C++ code needs. In some environments, the MPI compiler wrapper handles this automatically, but Cygwin's setup sometimes requires explicit instruction.
内容的提问来源于stack exchange,提问作者spyros

