Search code examples
c++windowsmakefile64-biti386

Detecting i386 vs x64 for Windows with C++ Makefile.win?


Sorry if this question is obvious, but I normally don't compile C++ on Windows:

I'm trying to write a Makefile.win whereby I need to link with a specific *dll. In the subdirectory of the library which needs to be linked, there's a /x64 version and a /i386 version, i.e.

.../libs/x64/library.dll
.../libs/i386/library.dll

For the Makefile for Linux, I was able to simply to link to the shared object via

SPECIAL_LIB= .../libs/library.so
LIBS=-L{SPECIAL_LIB}

but for windows, there is the 32-bit version i386 and the 64-bit version x64.

How could I detect whether the windows OS is 32-bit or 64-bit in the Makefile.win, and only link to the correct dynamic link library (and not the other)? Something like:

SPECIAL_LIB_32= .../libs/i386/library.dll
SPECIAL_LIB_64= .../libs/x64/library.dll
## check if 64-bit somehow
ifeq ($(strip $(OS)), "64bit machine")
        LIBS=-L{SPECIAL_LIB_32
endif

## check 32-bit
ifeq ($(strip $(OS)), "32bit machine")
        LIBS=-L{SPECIAL_LIB_64
endif


Solution

  • Assuming your target is the host machine, I believe you'll have to rely on environment variables. On my PC (Win10) > echo %PROCESSOR_ARCHITECTURE% yields AMD64. As per this article it should be PROCESSOR_ARCHITEW6432, but that's not the case. Thus, the following should be pretty safe to use on multiple Windows versions:

    set(arch 0)
    ifeq ($(OS),Windows_NT)
        CCFLAGS += -D WIN32
        ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
            set(arch 64 FORCE)
        else
            ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
                set(arch 64 FORCE)
            endif
            ifeq ($(PROCESSOR_ARCHITECTURE),x86)
                set(arch 32 FORCE)
            endif
        endif
    

    And then simply concat ${SPECIAL_LIB} with ${arch} when linking the library, assuming they are defined as in your cmake.