Live data from Hacker News

A Simple Makefile for Medium-Sized C/C++ Projects

spin.atomicobject.com

31–40 of 120 posts

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#31
post #2

Neat! I've been putting off learning how to use those `-M` flags to automatically generate dependencies, but it looks like it's not that hard :) What I also like to do is hide the actual compilation command with recipes like this, inspired by the Linux kernel %.o : %.c @echo " CC $ I think it looks a lot tidier, it turns a ton of dense uninteresting command lines into a few dozen short lines.

In a past life, the mega-but-autogenerated Makefile I helped maintain would store the executed command and only echo it if the command failed. And we logged everything to file, so you got nice output but all the info was there if we needed it. Didn't take that long to work out how to do it, once we'd decided it would be a nice thing to have.

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#32

Just use cmake or scons. In 2016, you should not hand write a single makefile ! A simple cmake/sconcs build file will be as simple as the one shown (or even simpler !) and it will definitively will be easier to integrate external dependencies if you have to.

cmake generally works great, but the larger and more complex the project gets (and the more platforms you need to support), it gets exponentially uglier. I've had the displeasure of needing to maintain a single cmake infrastructure for a large (~500K or so lines plus 3rd party libs) project that targeted Windows, Linux, Mac, iOS, Android, and a few other obscure mobile platforms. Here was the progression: 1. Linux ta…

Would you write an article detailing your experience?

It sounds fun to read.

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#33
post #6

Just use cmake or scons. In 2016, you should not hand write a single makefile ! A simple cmake/sconcs build file will be as simple as the one shown (or even simpler !) and it will definitively will be easier to integrate external dependencies if you have to.

Would be nice if you could give an example, not just "use that".

I would not be using cmake if this repository did not exist:

https://github.com/Akagi201/learning-cmake

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#34
post #6

Just use cmake or scons. In 2016, you should not hand write a single makefile ! A simple cmake/sconcs build file will be as simple as the one shown (or even simpler !) and it will definitively will be easier to integrate external dependencies if you have to.

Would be nice if you could give an example, not just "use that".

# SConstruct file env = Environment() hello = Program(["hello.c"])

$ scons

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#35
post #16

It always annoys me that we don't have better solutions for building C and C++ projects by now. My own take on the generic drop-in makefile[1] has over 1000 stars on github, which says pretty loudly to me that this is a common pain point for many people. Alas I haven't had a brilliant idea for a solution (yet). [1]: https://github.com/mbcrawfo/GenericMakefile

True. Other languages have it way easier because:

- proper module system, none of this header file nonsense

- no compiler flags to worry about

- no preprocessor definitions to worry about

- as a consequence of the above, no dependencies that require their own linker flags, include directories and preprocessor definitions to work

- no need to support different compilers/platforms with different configurations (which work differently with libraries that require different linker flags and include directories and...)

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#36
post #29
post #10

Please don't brew your own build system. It likely won't handle at least one of the following situations: * installing into a DESTDIR * setting custom CFLAGS/LDFLAGS * changing prefix, libdir, sysconfdir * cross-compilation * parallel builds * conditional features * missing requirements and will make packaging your software in Linux distros much harder than it needs to be.

There’s no point telling people not to do something if you don’t give them an alternative that does do all those things. (And no, “just use autotools” is not a viable answer.)

Autotools does do all of those things. I'm not a particular fan of it either, but when I'm packaging software for a Linux distro then I'd rather see autotools or cmake being used instead of some crappy custom build system that will require patching to make it work.

There is room for someone to come up with a good build system. It had better do everything autotools and cmake do, and be well documented and widely used, so there's a rather large barrier to entry.

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#38
post #23

Earlier quoted context omitted.

I strongly disagree. The tab requirement for Make is very well known and less strange than custom Makefile generation. Simple Makefiles work extremely well in lots of projects.

Somebody listed a number of common build system requirements below, but of course there are usually many many more like custom compilers, cross-compilation, any number of things that your "simple makefile" can't work with. By the time the Makefile has grown to support all these things it no longer has the properties that made it useful in the first place (parsimonious, understandable, small).

Supporting custom compilers and cross‐compilation is as simple as using ${CC} in the Makefile instead of gcc…

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#39
"But because Make works backward from the object files to the source, we need to compute all the object files we want from our source files".

No.... don't do this.

Make is intended to work the other way around... it finds the source from your targets.

Define your VPATH and let it find them.

This is what loads of people do and is why there are so many Makefiles are packed full of macro-magic!

Re: A Simple Makefile for Medium-Sized C/C++ Projects

#40
post #29
post #10

Please don't brew your own build system. It likely won't handle at least one of the following situations: * installing into a DESTDIR * setting custom CFLAGS/LDFLAGS * changing prefix, libdir, sysconfdir * cross-compilation * parallel builds * conditional features * missing requirements and will make packaging your software in Linux distros much harder than it needs to be.

There’s no point telling people not to do something if you don’t give them an alternative that does do all those things. (And no, “just use autotools” is not a viable answer.)

Ehm ehm... every second reply around here is shouting "cmake"...
Post reply on HN