Search code examples
c++linuxmakefileconditional-compilation

Makefile, modify command based on the OS


After reading this question, I wrote a Makefile that starts with

CXX=g++
CXXFLAGS= -std=c++17  -Wall -O3 -g

ifeq ( $( shell uname ), "Linux" )
CXXFLAGS += -fopenmp
endif


LIBS= -pthread
INCLUDES = -I.
TARGETS= my targets...

I need to pass the -fopenmp flag only if I'm compiling on linux and not when I'm compiling on Mac.

My problem is that this don't work and the flag never get passed.


Solution

  • gmake's syntax is quite sensitive to whitespace (especially gmake macros). Additionally, the output of uname does not include quotes.

    This should be:

    CXX=g++
    CXXFLAGS= -std=c++17  -Wall -O3 -g
    
    ifeq ($(shell uname),Linux)
    CXXFLAGS += -fopenmp
    endif
    
    zz:
        echo $(CXXFLAGS)
    

    Result:

    $ make zz
    echo -std=c++17  -Wall -O3 -g -fopenmp
    -std=c++17 -Wall -O3 -g -fopenmp