Search code examples
cmakefilecross-compilinggnu-make

How to cross compile for ARM on Ubuntu conditionally?


I have written the following make file

all: writer.o

writer.o:
    gcc -Wall writer.c -o writer

clean:
    rm *.o

How do I add a functionality to this make file such that I am able to generate an application for the native build platform when GNU make variable CROSS_COMPILE is not specified on the make command line.However, when CROSS_COMPILe is set, I should generate a cross compiled output file using the compiler, aarch64-none-linux-gnu-gcc.


Solution

  • Set the CROSS_COMPILE variable itself to the compiler prefix. So for native builds:

    CROSS_COMPILE = 
    

    (i.e. there's nothing, the variable is "empty").

    And for cross-compilation:

    CROSS_COMPILE = aarch64-none-linux-gnu-
    

    Then set:

    CC = $(CROSS_COMPILE)gcc
    

    To complement the above, use implicit rules to build the program:

    all: writer
    
    writer: writer.o
    

    That's all that you need.