3 ms·
Regarding your Makefile; you should still pass CFLAGS through to the compiler when linking, not only LDFLAGS. Suppose CFLAGS contains -m32 (supported by an x86-
by kazinator 8y ago
Regarding your Makefile; you should still pass CFLAGS through to the compiler when linking, not only LDFLAGS. Suppose CFLAGS contains -m32 (supported by an x86-64-targetted GCC to do 32 bit). You compile the .o files with that, but then link without it, which fails trying to make a 64 bit executable out of 32 bit .o's. Some crazy distros pass a --sysroot in CFLAGS; if you don't have that, your build finds the wrong library and header files.
Even when linking, don't call "cc", but "$(CC)". You're relying on the implicit rule to compile your .c to .o which will use $(CC). $(CC) could be some ARM cross-compiler supplied by a distro. When you link using "cc", you end up using the build-machine's native compiler.
Write the build system like this is an awesome program that major distros will be eager to pick up, and make it easy for the package maintainer. :)
Speaking of CFLAGS, only set that conditionally; don't clobber it:
CFLAGS ?= -O2 # Only if CFLAGS is not specified externally, use this default
for things that your program needs in order to build right, put that in other variables of your own:
DIALECT_CFLAGS := -std=c11
CFLAGS += $(DIALECT_CFLAGS) # integrate external CFLAGS with our own.
Same deal with LDFLAGS. Both CFLAGS and LDFLAGS can include important things that cause a bad build if you mess with them.
- Zelizz 8y agoWhere would one go to find more of this conventional Makefile wisdom? I've had so many issues trying to use make the "right" way (flexible, clean, terse, etc.). I feel like one of the best ways to acquire this wisdom is to post a project with lots of mistakes and let people tear it apart.
- kazinator 8y agoI got it from working on lots of C for almost thirty years, and also by putting together a from-scratch embedded GNU/Linux distro some decade ago, and more recently doing distro work also. As far as knowing GNU Make, I recommend just reading the manual from beginning to end, perhaps twice.
- simonblack 8y agoI bought what seemed to be overstock or maybe it was just being remaindered in a 50%-off Uni Bookshop sale about 20 years ago. It's a tiny (80 page) little book called "Managing Projects with make" by Steve Talbot and printed by O'Reilly and Associates. Most of what I know about 'make' I learned from this book.
- emmelaich 8y agohttps://nostarch.com/gnumake https://nostarch.com/gnumake
- teddyh 8y agoThat is not, contrary to what the name might imply, the official GNU Make manual. That one can be read here, for free: https://www.gnu.org/software/make/manual/ https://www.gnu.org/software/make/manual/
- emmelaich 8y agoI don't take the name as implying that. Anyway, it is excellent and has some good advice and tips. Unlike the official manual which is more a reference.