Update CFLAGS or LDFLAGS in a makefile depending on the current OS

I am trying to create a makefile that will work on both OSX and Linux.

My problem is that I need to change cflagsand ldflags, depending on the OS, the make file executed, but I cannot get it to work. This is my makefile:

OS:=$(shell uname)
DST=hello
SRC=$(wildcard *.cpp)
OBJ=$(SRC:.cpp=.o)
CFLAGS=

all: clean DetectOS $(DST)

DetectOS:
ifeq ($(OS),Darwin)
    @echo OS : $(OS)
    CC=g++
    LDFLAGS="-lm -framework OpenCL"
    CFLAGS+=-O3
endif

ifeq ($(OS),Linux)
    #Coming soon...
endif

$(DST): $(OBJ)
    $(CC) -o $@ $^ $(LDFLAGS)

%.o: %.cpp
    $(CC) -o $@ -c $< $(CFLAGS)

clean:
    rm -rf *.o $(DST)

But when I run this code, neither cflags, ldflagsor CCis updated in the conditional block ifeq. I get the following result:

$ make
rm -rf *.o hello
OS : Darwin
CC=g++
LDFLAGS="-lm -framework OpenCL"
CFLAGS+=-O3
cc -o opencl.o -c opencl.cpp 
cc -o hello opencl.o 
Undefined symbols for architecture x86_64:....

As you can see, the OS was detected because we entered the conditional block ifeq, but it CCdoes not update and saves an uninitialized value CC. Finally, the linker process crashes because OpenCL does not reference ldflags.

, LDFLAGS="-lm -framework OpenCL", :

LDFLAGS=-lm -framework OpenCL
/bin/sh: -framework: command not found
make: *** [DetectOS] Error 127

( stackoverflow) .

Mac OS X Yosemite.

+4
1

, ifeq make, makefile ( :).

.. :

OS:=$(shell uname)
DST=hello
SRC=$(wildcard *.cpp)
OBJ=$(SRC:.cpp=.o)
CFLAGS=

ifeq ($(OS),Darwin)
    $(info OS is $(OS))
    CC=g++
    LDFLAGS=-lm -framework OpenCL
    CFLAGS+=-O3
endif

ifeq ($(OS),Linux)
    #Coming soon...
endif

all: clean $(DST)

...

( "DetectOS", )

+3

Source: https://habr.com/ru/post/1619278/


All Articles