Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit Intermediate Object Files in GNU AutoMake

I have a C application I'm converting from a set of hand coded Makefiles to GNU AutoMake. It has a subdirectory that contains an interface header and several platform-dependent implementations. Currently an implementation is selected and built to an object file with a fixed name in that directory. The code that uses the driver interface then links against that object file and automagically gets the right driver.

What's the best way to convert this system to use AutoTools? I already have AutoConf creating a substitution for the correct driver implementation file. I just can't figure out how to get an AutoMake target whose EXTRA_*_SOURCES and *_LDADD variables I can put the implementation files in. Do I just need to give up on the intermediate object file and put them in the list for the program target that uses them?

EDIT: Solved thanks to ptomato.

Because of Automake's dependency resolution all possible sources must be named in *_SOURCES and EXTRA_*_SOURCES, as explained in the Conditional Sources section of the manual. Therefore, the library stanza is actually:

noinst_LIBRARIES = libdriver.a
libdriver_a_SOURCES = driver.h
EXTRA_libdriver_a_SOURCES = driver-linux.c driver-windows.c driver-osx.c
libdriver_a_LIBADD = @DRIVERIMPL@
like image 705
Sam Hanes Avatar asked Feb 04 '26 22:02

Sam Hanes


1 Answers

You could build a convenience library, and link it statically to your application. There's ultimately very little difference between that and an object file. See this page of the Automake manual. Basically it goes like this:

noinst_LIBRARIES = libdriver.a
libdriver_a_SOURCES = @CORRECT_IMPLEMENTATION_FILE@
myprogram_LDADD = libdriver.a

where your intermediate object file with a fixed name is libdriver.a, and your application is myprogram.

However, this seems unnecessarily complicated to me. I don't see any reason why you shouldn't, as you suggest, give up on the intermediate object file and put the implementation files in the program target that uses them.

like image 65
ptomato Avatar answered Feb 09 '26 00:02

ptomato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!