Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: How to compile several C files Date: Tue, 07 Feb 2017 16:19:38 +0000 Organization: A noiseless patient Spider Lines: 43 Message-ID: <87wpd2as85.fsf@bsb.me.uk> References: <87a89yca76.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="6b16403c060c76fc53ae0c0bdb247f77"; logging-data="28520"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Re/IXMmYdJm9lDPK7acHboqxbDTj2v/I=" Cancel-Lock: sha1:8jXJRnVKzCepbvDr6p1lryST5aQ= sha1:D78Q8ku04MqkJSxVlkx7TWK0Uuo= X-BSB-Auth: 1.9502ad98574966732db0.20170207161938GMT.87wpd2as85.fsf@bsb.me.uk Xref: csiph.com comp.lang.c:103373 Richard Heathfield writes: > On 07/02/17 15:06, Ben Bacarisse wrote: >> Once I have two files, I write a makefile. > > I'm slightly lazier than that. Once I have /one/ file, I write a > makefile. At a pinch, you can be even lazier when there is only one file because you might be able to get away with no makefile at all because, in an otherwise empty directory, "make x" will compile and link x.c (if it exists). For me, the next stage is to put (for example) CFLAGS=-std=c99 -Wall -pedantic into "makefile" and nothing else. That lets you "make x" and/or "make x.o" with the right flags for any C files you have in that directory (for different "x" of course). A very simple solution for quick one-file tests. > To be more precise, I'm even lazier than /that/ - I run a program to > write the makefile on my behalf: > > makegen foo > Makefile Since that is (I think) your own program, it is probably more helpful to know about "gcc -MM *.c". This will write out the dependencies between files. To finish off a simple (GNU) makefile it helps to know one magic rule: program: one.o two.o other.o gcc -o $@ $^ where $@ means the target ("program") and $^ means the dependencies ("one.o two.o three.o"). You can write it out of course, but knowing $@ and $^ makes it the same rule every time. -- Ben.