Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple makefile for C/C++ targets used with arm-linux-gcc

I would like to cross-compile a simple program for ARM architecture using the arm-linux-gcc suite of compilers [arm-linux-gcc (Buildroot 2011.08) 4.3.6]. I've attempted to use a simple makefile for compiling C code, and another simple makefile for compiling C++ code. For example, my makefile for C code is reproduced below, but it does not create an ELF binary for running on my embedded system. The host system is x64 GNU Linux.

Here is the listing of my very simple makefile for a C program:

CC=arm-linux-gcc
CFLAGS=-Wall
main: test.o 

clean:
    rm -f test test.o 

The makefile reproduced above only creates an object file with extension .o, and does not create an ELF binary.

I've Googled for a good solution, but I can't seem to find one webpage showing example cross-compile ARM makefiles for both C and C++ programs. Perhaps an answer to this post could show such examples.

like image 547
Nicholas Kinar Avatar asked Jan 04 '12 18:01

Nicholas Kinar


People also ask

How do I make a simple makefile?

By putting the object files--hellomake.o and hellofunc.o--in the dependency list and in the rule, make knows it must first compile the . c versions individually, and then build the executable hellomake. Using this form of makefile is sufficient for most small scale projects.

How do you cross-compile arms with GCC?

Save this answer. Install gcc-arm-linux-gnueabi and binutils-arm-linux-gnueabi packages, and then just use arm-linux-gnueabi-gcc instead of gcc for compilation. This brings in the complete cross-compile environment, including binutils. On Ubuntu 13.10 you get gcc-4.7 for 'gnueabi' and gcc-4.8 for 'gnueabihf'.

Can GCC compile for ARM?

Use GCC to cross-compile binaries for different architectures from a single build machine. If you're a developer creating binary packages, like an RPM, DEB, Flatpak, or Snap, you have to compile code for a variety of different target platforms. Typical targets include 32-bit and 64-bit x86 and ARM.

How does makefile work in C?

Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.


1 Answers

Have a look at the GNU make manual (info make), Section 10.2. It has a catalogue of the implicit rules, i.e. the rules where you don't need to explicitly state the commands. Like @GregHewgill thought, the "Linking a single object file" implicit rule builds N from N.o, but the name must match. Therefore, you can either name your executable like your object file, in which case

test:

or (more standard because it defines the all target)

all : test

completely suffice. You can also write out the rule explicitly, like Greg Hewgill also described. In this case, the standard rule is:

 $(CC) $(LDFLAGS) N.o $(LOADLIBES) $(LDLIBS)

Include the LDFLAGS and LDLIBS in your Makefile, it makes life easier for users.

(sic: I think LOADLIBES is really LOADLIBS, and the author missed the -o).

Overall, I'd recommend autoconf and automake instead of hand-rolling makefiles. Gives you a bunch of Makefile features for very little work.

like image 168
thiton Avatar answered Oct 26 '22 13:10

thiton