Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One makefile for two compilers

I have two makefiles, for native and cross compilation. The only difference between them is compiler name:

# makefile
CC = g++
...
# makefile-cc
CC = arm-linux-gnueabihf-g++
...

To make native compilation, I execute make, to make cross-compilation, I execute make -f makefile-cc. I want to have one makefile, which should be executed using make for native compilation, and make cross for cross-compilation. What is correct syntax to do this, something like:

# makefile (C-like pseudo-code)
if cross
    CC = arm-linux-gnueabihf-g++
else
    CC = g++
like image 844
Alex F Avatar asked Nov 13 '12 09:11

Alex F


2 Answers

You can assign/append variables for specific targets by using the syntax target:assignment on a line. Here is an example:

native: CC=cc
native:
    echo $(CC)

cross: CC=arm-linux-gnueabihf-g++
cross:
    echo $(CC)

calling

make native

(or just make, here) prints

echo cc
cc

and calling

make cross

prints

echo arm-linux-gnueabihf-g++
arm-linux-gnueabihf-g++

So you can use your usual compilation line with $(CC)

like image 63
bernard paulus Avatar answered Nov 10 '22 01:11

bernard paulus


You can pass parameters to make.
e.g. make TARGET=native and make TARGET=cross then use this

ifeq ($(TARGET),cross)
        CC = arm-linux-gnueabihf-g++
else
        CC = g++
endif
like image 23
Anon Avatar answered Nov 10 '22 00:11

Anon