3

I have gone through the link Passing additional variables from command line to make.

I have a project which compiles both on Linux and Windows using makefiles. In Windows it uses gcc while in Linux it uses the ARM version of gcc ie armv7-linux-gcc. I would like to use a command line variable which tells the makefile which compiler to use depending on Windows or Linux.

For example in Windows it should have something like this:

CC= gcc  
CFLAGS= -c -D COMPILE_FOR_WINDOWS

and for Linux:

CC =  armv7-linux-gcc  
CFLAGS = -c -D COMPILE_FOR_LINUX

These preprocessor defines COMPILE_FOR_WINDOWS and COMPILE_FOR_LINUX are present in the code base and can't be changed.

Also for make clean it should clean up both for Windows and Linux. I can't assume that I people who build this will have Cygwin installed so can't use rm for deleting files.

Community
  • 1
  • 1
user1377944
  • 425
  • 2
  • 5
  • 12
  • Look here: http://stackoverflow.com/a/10608327/635608 – Mat May 20 '12 at 10:47
  • 2
    Since standard [`make`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html) has no conditional mechanisms in it, anything you do as conditionals in the makefile is somewhat platform specific. If you aren't going to mandate `rm`, presumably you aren't going to mandate GNU `make` either, so you have just made it very difficult for yourself. Consider separate makefiles, or makefile fragments, for the two configurations, so you end up running `make -f makefile.unix` or `make -f makefile.win64` or whatever. Minimize the differences, but you need to know what Windows make supports. – Jonathan Leffler May 20 '12 at 14:49
  • 1
    Just to be clear, which version(s) of Make are you using? – Beta May 21 '12 at 20:30
  • Maybe you should move towards makefile generators like cmake? – bcelary Jun 19 '12 at 10:03
  • 1
    What is the question? If it is about rm, as you have answered everything else yourself. Then do it the same way. `RM=rm` or `RM=del` – ctrl-alt-delor Jun 27 '12 at 15:28

2 Answers2

1

This answer is only valid if you're using GNU make or similar:

Conditionally set your make variables using an Environment Variable.

For the 'clean' rule to function properly, you may also have to create a make variable for any differences in file extensions for either OS.

Naive example:

ifeq ($(OS), Windows_NT)
  CC=gcc
  RM=del
  EXE=.exe
  CFLAGS=-c -DCOMPILE_FOR_WINDOWS
else
  CC=armv7-linux-gcc
  RM=rm  
  EXE=
  CFLAGS=-c -DCOMPILE_FOR_LINUX
endif

PROG=thing$(EXE)

.PHONY: all
all: $(PROG)
        $(CC) $(CFLAGS) -o $(PROG) main.c

.PHONY: clean
clean:
        -$(RM) $(PROG) *.o
analogue_G
  • 101
  • 4
0

Maybe you could use ant (with a build.xml file) to build the project. Else, in a Makefile, you would need to check the system and put some conditions to check wether you are making the project in an Unix environment or a Windows environment.

Nuno Aniceto
  • 1,892
  • 1
  • 15
  • 16