LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 05-31-2012, 03:27 PM   #1
Enoch247
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
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
 
Old 05-31-2012, 06:28 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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?
 
Old 05-31-2012, 10:45 PM   #3
merlinblack
LQ Newbie
 
Registered: Dec 2007
Location: Sydney, Australia
Distribution: Ubuntu, Ubuntu Server, Linux From Scratch, Fedora, Mac OSX, and Windows sometimes.
Posts: 20

Rep: Reputation: 1
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)
Code:
somelib: bla.o bla2.o

cli: somelib cli.o somecommonstuff.o

gui: somelib gui.o somecommonstuff.o

all: cli gui
I have not tried having a file re-compiled for different targets. My guess is you could do something like:
Code:
somecommonstuff_for_cli.o: somecommonstuff.c
    CC CLI_FLAGS -DCLI $< -o somecommontuff_for_cli.o

somecommonstuff_for_gui.o: somecommonstuff.c
    CC GUI_FLAGS -DGUI $< -o somecommontuff_for_gui.o

cli: somelib cli.o somecommonstuff_for_cli.o
.....etc....
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.
 
Old 06-01-2012, 02:20 AM   #4
bdt-rob
LQ Newbie
 
Registered: Jun 2009
Location: Cambridge, England
Distribution: Gentoo, Arch, (xandross), (Slackware)
Posts: 16

Rep: Reputation: 2
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:

Code:
all: cli gui

cli: cli/app.o cli/somecommonstuff.o

gui: somelib gui/app.o gui/somecommonstuff.o

cli/%.o : %.c
         CC CLI_FLAGS -DCLI $< -o $@

gui/%.o : %.c
         CC GUI_FLAGS -DGUI $< -o $@
If you're doing a big, multi-platform project using automake and friends, they support this sort of thing:
Code:
bin_PROGRAMS = myctrl mymon
sbin_PROGRAMS = lminst

myctrl_SOURCES = ctrlmain.cc Auth/lm.cc
myctrl_CXXFLAGS = -DRCFILE="\"/blah/blah/$(rcfile)\""
myctrl_LDADD = Gui/libgui.a Src/libctrl.a Comms/libcomms.a

mymon_SOURCES = monmain.cc
mymon_LDADD = Gui/libgui.a Src/libmon.a Comms/libcomms.a

lminst_SOURCES = Auth/lminst.cc Auth/lm.cc
lminst_CXXFLAGS = -DSHAREDIR="\"$(mydir)/\""
What that resolves to is a more complex (and far less readable) form of MerlinBlack's hand-written concept.
 
1 members found this post helpful.
Old 06-01-2012, 10:01 AM   #5
Enoch247
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
man/Makefile.am:11: `%'-style pattern rules are a GNU make extension aibutter Linux - Networking 3 06-24-2011 07:03 AM
link-time libs need to be run0time libs says curl's make install UniquelyAm Linux - Software 0 06-09-2007 01:40 AM
Compiling multiple .c file using single command in makefile vipulc Linux - General 2 03-18-2006 11:49 PM
create GNU make Makefile from visualstudio project andreyk Programming 0 02-02-2005 10:25 AM
Installing libs from makefile bigredgiant1 Programming 1 08-06-2004 03:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration