Makefiles: processing multiple object files

Started by
0 comments, last by nobodynews 14 years, 5 months ago
Hi: It has actually been a while since I don't code any C++ since my professional life has shifted towards web development... Really, php and &#106avascript code can actually make you forget how to use the old C++! Anyway, still I try to code some C++ in my spare time, for the sake of learning - still an absolute beginner here - and for my own pleasure. Recently I came up with a little problem with my makefiles... Say that I have separate source and header files that accomplish isolate tasks. I am using the SDL library: -screen.cpp, for my screen related functions, encapsulated within a class. -functions.cpp, for miscellaneous static functions (just for the sake of having them within a class). My makefile could go like this:

result: objects/screen.o objects/functions.o sources/main.cpp
	g++ -Wno-deprecated -o resultado sources/main.cpp objects/screen.o objetos/functions.o -lSDL

#Screen...
objects/screen.o: sources/screen.cpp headers/screen.h
	g++ -Wno-deprecated -c -o objects/screen.o sources/screen.cpp -lSDL

#Functions
objects/functions.o: sources/functions.cpp headers/functions.h
	g++ -Wno-deprecated -c -o objects/functions.o sources/functions.cpp -lSDL



So, my main result would depend on both screen.o and functions.o. Say that it is absolutely predictable that my program would grow with a... camera, for example. Still related to graphics, but something I would like to stand on its own.. My makefile then:

result: objects/screen.o objects/functions.o objects/camera.o sources/main.cpp
	g++ -Wno-deprecated -o resultado sources/main.cpp objects/screen.o objects/camera.o objects/functions.o -lSDL

#Camera...
objects/camera.o: sources/camera.cpp headers/camera.h
	g++ -Wno-deprecated -c -o objects/camera.o sources/camera.cpp -lSDL

#Screen...
objects/screen.o: sources/screen.cpp headers/screen.h
	g++ -Wno-deprecated -c -o objects/screen.o sources/screen.cpp -lSDL

#Functions
objects/functions.o: sources/functions.cpp headers/functions.h
	g++ -Wno-deprecated -c -o objetos/functions.o sources/functions.cpp -lSDL



Say then that my program keeps on growing on the graphics until I have like ten separate headers... My "result" file should depend and link against lots of object files. My program could also grow on other respects, like, for example, multiple structures that I should use, each one on its own source file but all related. The most obvious example is different kinds of classes contained within a simple video game: all related to the "game" but "apart" from the graphics... That should make my "result" file link against a whole lot more of object files!. For the sake of making it manageable I was wondering if I could, somehow, take all the "graphic" object files and put them together in a single "group_graphics.o". Same with all the "game" objects: all within a single "map_related.o" file. I know that I could just g++ them together into a single object from their sources but, is there something I can do with their objects?. I am aware that, from the point of view of the makefile, it makes no difference at all since a single change in a source and my hipotetical "all_in_one.o" file has to be processed again but still I cannot but think that I could manage my work better if I could do as I ask. Frankly, I am not sure if my question makes sense at all from the point of view of the veteran coder but I will appreciate all answers and suggestions. Thanks a lot in advance.
Advertisement
Perhaps what you want is to create a library. I've never done it with gcc, but this might be the proper way to go about it.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement