-1

I had written a makefile that can run a program (which consists of few .c files, .h files, and the main function) on the Ubuntu platform. Now, I want to execute the same program on the ARM cortex board. I have installed the ARM compiler too on my PC. Now, what changes do I need to make in the make file so that the code runs on the ARM board? How to set the compiler flags?

*I have a compiler that is similar to the compiler which is used on the ARM board.

My makefile is as follows:

CC = gcc
csrc = $(wildcard inc/*.h) \
   $(wildcard src/*.c) \
   $(wildcard main.c)
obj = $(csrc:.c=.o)
hellomake: $(obj)
    $(CC) -o $@ $^ -lm -Wall
.PHONY: clean
clean:
    rm -f $(obj) hellomake *.o
sepp2k
  • 363,768
  • 54
  • 674
  • 675
rkc
  • 111
  • 8

3 Answers3

3

Assuming that you have the right compiler for ARM, then simply supplying a different CC= should suffice:

make CC=gcc-arm

(assuming your ARM compiler is called gcc-arm). You could also edit this in the makefile itself.

Remember to make clean before compiling with a different compiler because make does not know with which compiler might have generated existing files.

I also want to know regarding how to set the ARM compiler flags. Can I include those flags directly in the normal LDFLAGS section?

Well, if you want to give compiler flags you should give those in CFLAGS. The LDFLAGS variable is for linker flags. See here for more information on implicit variables. In your case there is basically no difference since you would pass both of these to the $(CC) command, but it's not always the case.

You should modify your compilation command like this:

LDFLAGS = -lm
CFLAGS = -Wall

# ...

hellomake: $(obj)
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

Then you can supply different flags from the command line (overriding those in the makefile):

make CC=gcc-arm CFLAGS='-Wall -mfpu=neon-vfpv4 -mcpu=cortex-a7'

Or you could just modify the makefile itself:

CFLAGS = -Wall -mfpu=neon-vfpv4 -mcpu=cortex-a7

As a note: make already has an implicit value for $(CC), which is cc. The cc command is usually a link to your default compiler on Linux. Therefore you can even omit the CC = gcc in the makefile altogether if you want.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • I also want to know regarding how to set the ARM compiler flags. Can I include those flags directly in the normal LDFLAGS section? – rkc Feb 06 '20 at 13:51
  • Thanks for the explanation. How to add the definitions like -mfpu=neon-vfpv4, -mcpu=cortex-a7? How do I add these kinds of definitions? – rkc Feb 06 '20 at 14:14
  • Actually `-lm` is a library switch and should be put after object files. The built-in rule (which could be reused in `hellomake` recipe) is `$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@`, where switches like `-lm` should be placed in `LDLIBS`. – raspy Feb 06 '20 at 14:35
  • @RamakrishnaChaitanyaRachumal you can place target-specific flags in `TARGET_ARCH`. It is used in both `COMPILE.c` and `LINK.o` rules (and most others too) – raspy Feb 06 '20 at 14:36
  • @RamakrishnaChaitanyaRachumal you can add those to `CFLAGS` without a problem from command line: `make CFLAGS='-mfpu=neon-vfpv4 -mcpu=cortex-a7'`, or inside the makefile directly: `CFLAGS = -mfpu=neon-vfpv4, -mcpu=cortex-a7` – Marco Bonelli Feb 06 '20 at 14:43
  • @raspy please don't confuse things with implicit variables that don't even exist. `$(LINK.o)` means nothing in `make`. `LOADLIBES` is also deprecated, it's the old name of `LDLIBS`. – Marco Bonelli Feb 06 '20 at 14:46
  • @MarcoBonelli..Thanks, I did as you suggested. I'm able to execute the code on the ARM board now. – rkc Feb 07 '20 at 05:04
  • @MarcoBonelli they do exist. Just run `make -p` and check the output. It is defined in https://git.savannah.gnu.org/cgit/make.git/tree/src/default.c#n269 and https://git.savannah.gnu.org/cgit/make.git/tree/src/default.c#n599. – raspy Feb 07 '20 at 10:53
  • @RamakrishnaChaitanyaRachumal you're welcome. If I answered your question you can use the checkmark on the left to accept my answer and mark your question as resolved. – Marco Bonelli Feb 07 '20 at 11:48
  • @raspy interesting, that I did not know. I didn't see them in the doc so I assumed those were some kind of specific variables for some other version of make. – Marco Bonelli Feb 07 '20 at 11:50
0

You need a makefile for cross-compilation like described in How to modify Makefile to support cross compilation?,

so

CC=gcc-arm
ralf htp
  • 9,149
  • 4
  • 22
  • 34
0

Based on comments...

You appear to be wanting to build something on say linux on some arm board that has a compiler on it. Say for example a raspberry pi running some flavor of linux and you are starting with a Makefile used on say some linux on an x86.

You are done, do not make any modifications, if you feel the need to make modifications then you will likely need to make them on the x86 version as well.

If you are talking about cross compiling which my first draft assumed, then that is a different story, but from the question and other items it does not sound like that is your target.

old_timer
  • 69,149
  • 8
  • 89
  • 168