Sign in to follow this  
Lord Gzoo

generating a list of needed files for use in makefiles

Recommended Posts

In my makefiles, and other makefiles I've seen, there's always this kind of line:
$OBJECTS=main.o xxx.o yyy.o
So, once you want to add another file or class or whatever to you project you have to manually add it to the makefile. For example, I want to add zingabo.cpp to my project so I have to edit the above line to:
$OBJECTS=main.o xxx.o yyy.o zingabo.o
Is there a way to automatically do that? (Without using an IDE or my nice Python script) I've also read a little about the GNU autotools and from what I understood you still have this kind of problem only in a different manner, i.e. you still have to manually edit some file whenever a new file is added to the project. Please enlighten me. P.S. I'm using Vim under GNU/Linux.

Share this post

Link to post
Share on other sites
A minimal example of what I have in my makefiles

CC = g++ #compiler name
BIN = demo #binary name
SUBDIRS = . #list of source directories
SRC = $(foreach dir, $(SUBDIRS) , $(wildcard $(dir)/*.cpp)) #generate the list of source files
OBJ = $(SRC:.cpp=.o) #generatethe list of obj files

all: $(BIN)
$(CC) $(OBJ) -o $@

%.o : %.cpp
$(CC) -c $< -o $@

You can also use cmake, which can do the same thing.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this