I'm trying to create a makefile that will works on both OSX and Linux.
My problem is I need to change cflags
and ldflags
depending on OS the makefile is executed but I can't make it work. That's 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
, ldflags
or CC
are updated in the ifeq
conditional block. 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 is detected because we went in the ifeq
conditional block, but CC
isn't updated and keep a non-initialized value of cc
. Finally the linker process fails because OpenCL is not referenced in ldflags
.
An other little point, if I don't put quotes in LDFLAGS="-lm -framework OpenCL"
I get the error :
LDFLAGS=-lm -framework OpenCL
/bin/sh: -framework: command not found
make: *** [DetectOS] Error 127
And based on multiple exemples (here on stackoverflow) I should do it without quotes.
I'm currently on Mac OS X Yosemite.
I think the ifeq block should not go into the make target, but just to the front of the makefile (before all:).
I.e. like this:
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)
...
(no "DetectOS" target, using info instead of echo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With