c++gccmakefilepathcross-compiling

GCC fails when multiple different versions are needed in makefile


I have a makefile that needs to use 2 different versions of GCC so as to output different executables in the same invocation. Specifically, I'm using one that targets old 32-bit Windows computers and one that targets much more recent computers. Unfortunately switches such as -m32 etc. by themselves do not cut it and I really do need 2 different versions of GCC.

I get strange errors when I add both versions to PATH and call them directly in the makefile. This was theorised as being attributable to GCC's modular design. The basic idea of my makefile is as follows:

GCC_32 = i686-w64-mingw32-gcc
GCC_64 = x86_64-w64-mingw32-gcc

win_32_bit: $(COMMON_REQUIREMENTS_NOT_IN_EXAMPLE)
    $(GCC_32) -mwindows -m32 -march=pentium $^ old_main_file.cpp -static-libstdc++ $(LIBS_NOT_IN_EXAMPLE) -o old.exe
win_64_bit: $(COMMON_REQUIREMENTS_NOT_IN_EXAMPLE)
    $(GCC_64) -mwindows -m64 -march=core2 $^ new_main_file.cpp -static-libstdc++ $(LIBS_NOT_IN_EXAMPLE) -o new.exe

If I were to instead remove all references to GCC from PATH and then hardcode the paths in, the code then works.

GCC_32 = /mingw32/bin/i686-w64-mingw32-gcc
GCC_64 = /mingw64/bin/x86_64-w64-mingw32-gcc

# The rest is the same

Obviously, hard-coding paths is bad and not portable. I'd rather find out why there's a conflict and write a makefile that deals with it. Perhaps there's a way to e.g. hide the other compiler from each other or perhaps glean the paths from PATH, remove them, and inject them in to the makefile


Solution

  • You can ask the shell to compute the paths for you:

    GCC_32 := $(shell which i686-w64-mingw32-gcc) 
    GCC_64 := $(shell which x86_64-w64-mingw32-gcc)
    

    But that is only masking the problem of it not working without absolute paths...