Advertisement

Thoughts on make and makefiles

Started by May 09, 2005 04:54 PM
4 comments, last by Kylotan 19 years, 6 months ago
Today I looked at several existing makefiles, and several makefile tutorial pages, and still couldn't come up with something useful. In the end I gave up and wrote a shell script instead:
#!/bin/sh
g++ -Wall -c *.cpp
g++ -lsqlite3 -o kyloprog *.o

Trying to find the correct syntax to do that using a makefile was getting me nowhere, since there seems to be several different ways to use wildcards, and numerous amounts of line noise... er, I mean punctuation that you need to distinguish between one bit and the next. Is there a quick replacement for what I have above? And has anyone had better luck with other build tools, like Jam?
I'd recommend you check out SCons, I've used make a lot, then played with Jam and SCons. I found SCons to be by far the easiest, and I like that an SCons script is basically a Python script so you can do anything with them, but it's also very easy to make simple ones.


Advertisement
Make doesn't like it when it doesn't know the names of all of the files you want to build. This really isn't a big deal since projects rarely add files once they're in full swing. So knowing all of the object files you want to build, just use something like this:

OBJECTS= first.o second.o main.o classesthatrule.oall: kyloprogkyloprog: $(OBJECTS)  g++ -lsqlite3 -o $@ $+%.o: %.cpp g++ -Wall -c $< -o $@


(keep in mind that Makefiles require tabs instead of spaces, they just didn't want to paste right)

the %.o : %.cpp rule does the magic of finding cpp files, i.e. first.cpp when building first.o
On my current game project we are using CMake. This is a cross-platform build tool that will generate (among others) makefiles for unix and Visual Studio project files for Windows. I find that the CMake files are a lot easier to use than to manually edit makefiles.

There is also another option of autoconf/automake for unix that will make your life easier than writing makefiles manually. I would recommend taking a look at CMake though. After using it for only a short while it feels very comfortable.
Assuming you're using GNU make, have you read the manual? I found it much more useful than any of the tutorials I found.

If you want to build all .c source files in a directory, you want to set up a variable like this:
SOURCE=$(wildcard *.c)OBJS=$(sources:.c=.o)
Free Mac Mini (I know, I'm a tool)
cozman - I'm checking out SCons now. It looks quite useful.

Apocryphiliac - Sadly, I add source (and hence) object files often, and currently have over a hundred in the project. Adding them one by one didn't appeal to me.

igni ferroque - Personally I found the manual to be confusing. It was after reading section 4.4 (Using Wildcard Characters in File Names) that I gave up on make because of all the different cases. Reading what should have been an enlightening chapter, I asked myself: do I use *.c or .c or %.c? What exactly does the 'wildcard' word do? Why is an unmatched wildcard used verbatim instead of ignored? Why throw in the 'patsubst' keyword when trying to give a simple example, without explaining which parts of the example pertain to 'patsubst' specifically? I think that documentation is fairly poor.

This topic is closed to new replies.

Advertisement