GNU make best practices; single makefile, multiple programs or libs
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
GNU make best practices; single makefile, multiple programs or libs
I have a reoccurring problem for which I haven't been able to find a best/common/accepted way of handling when using make with g++. Sometimes in a project I may want to have multiple outputs built from the same sources, but maybe only have a few minor differences(and therefore aren't really separate projects). Make seems really geared towards building only a single program or lib per makefile/project from what I have seen.
An example might be two programs that are built from the same source, but maybe use the code differently (different main func and as a result maybe minor differences in what is included) I know sometimes this is handled by putting all the functionality in a single executable and linking to it with different names (hd/hexdumb, gcc/g++/etc), but sometimes programs are different enough that this is awkward. Another example would be developing a lib and a CLI for the lib together in the same project. They could be separate projects, but keeping them together under a single makefile allows the CLI to be updated whenever the lib's header changes. This allows the libs test scripts to test the updated lib via the newly complied CLI.
It is definitely possible to accomplish this, but it seems like when I do it, I am going against the grain and is usually more difficult. Generally makefiles have one set of linker flags, compiler flags, object list, etc.
Is there a good/accepted way to do this or is doing so a bad practice that I missed somehow? I like sticking to established standards whenever possible.
Last edited by Enoch247; 05-31-2012 at 03:52 PM.
Reason: spelling
Makefile's can descend from the main directory to run other makefile's in sub-directories.
Have you considered the possibility of structuring this as a collection of dependent sub-projects?
Distribution: Ubuntu, Ubuntu Server, Linux From Scratch, Fedora, Mac OSX, and Windows sometimes.
Posts: 20
Rep:
It depends on how different the programs are, and if some files need to be compiled differently for each, i.e. with different defines for example. If each file is only compile once for everything it's not too hard at all. You would have something like:
(omitting the actual lines that do any work)
This might be Ok for small projects. Otherwise you'll want to split each program out into their own subdirectory, with their own (sub) makefile. The source still be shared amongst them of course, but the intermediate files can go into the subdirectories.
MerlinBlack's approach looks good, certainly for small projects. If you need to compile the same source but with different options, then you'll need to keep the two object files separate, either by the method already given, or using directories:
I like the per dir flags method. Maybe I'll run with that. I'm just surprised that there are no "cookbook" examples out there for this kind of pattern. I thought maybe it was because doing so violated some kinda of best practice.
I tried recursive make originally, but quickly realised why this practice is falling out of favour. Currently I have something similar to MerlinBlack's first example but I have outgrown this approach.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.