Live data from Hacker News

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

spin.atomicobject.com

51–60 of 120 posts

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

#51

This approach is flawed because it uses "find" to discover the source files which means that if you delete a source file, it won't cause a rebuild since it won't see any changed files.

Also you can't conditionally build a .c or .cpp file without ifdefs in the source.

Also you have to wait for find to run before any building starts. Can be slow for a large project.

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

#52

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.

Please do not use scons in your projects. Poorly written SConstructs are as common as poorly written Makefiles, and are more difficult to patch to work correctly because of all the magic that goes on under the hood.

SCons has many issues that negatively affect packagers. Some are listed on the Gentoo wiki here: https://wiki.gentoo.org/wiki/SCons

As an OpenBSD packager, I hit many of those issues and a few more. For example, scons’s C preprocessor support is not great, and often brings in header dependencies that should be #ifdef'd out; when those unnecessary headers are removed during build (happens often on a machine bulk building packages), the build breaks.

I’ve replaced SConstructs with Makefiles before. Typically it makes the build faster, simpler, and more portable, and is popular with other OS packagers. Example: https://github.com/sinamas/gambatte/pull/6

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

#53
post #38

Earlier quoted context omitted.

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…

This is how we got into this mess in the first place. "Surely it can't be very difficult, let me just add this.."

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

#54
post #41
post #36

Earlier quoted context omitted.

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,…

Auto tools is great. To compile all you do is ./configure make make install It couldn't be simpler. Even though I am part of the GitHub generation, I don't see why so many of my peers hate it. I actually like it.

autotools is pretty great for the end-user who wants to compile your code or the distributor who wants to package it up for Debian or whatever.

It’s a crawling horror for the programmer that wants to use autotools as the build solution for their code however, which is the topic under discussion.

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

#55
post #42

Earlier quoted context omitted.

Still hell of a lot simpler than hand-writing Makefiles though. For both small and large projects. I mean, a "hello world" CMakeLists.txt can be literally just this: add_executable(hello hello.c) And it's cross-platform, can generate Visual Studio/Xcode/... projects, etc.

I think we have to go deeper than that to really see an advantage of cmake. A corresponding makefile would be: all: hello That's not really complicated either.

Exactly. When evaluating a language / build system / whatever, it is definitely not sufficient to look at Hello World. You have to evaluate the worst case and average case if you want to get an idea of what you're in for. Looking at the "best case" (simple one-file project) tells you almost nothing except how hard it is to get started.

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

#56
post #41

Earlier quoted context omitted.

Auto tools is great. To compile all you do is ./configure make make install It couldn't be simpler. Even though I am part of the GitHub generation, I don't see why so many of my peers hate it. I actually like it.

This is kinda hilarious because, yes, "Autotools is great" [as a consumer]. But autotools and m4 is kinda awful to write IMO.

... and autotools is actively hate-inducing if you have to debug it.

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

#57
post #40
post #29

Earlier quoted context omitted.

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"...

CMake is icky. I’ve never been comfortable with it. I’ll take hand written makefiles over CMake, unless I have to have Windows as a build target.

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

#58

"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!

I do agree that VPATH is helpful, but you'd still need to put the Makefile into the build directory in that case, which is distasteful, as it's not a built file.

Also you can't really get around specifying the files or searching, unless you have one c file per executable.

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

#60
This is not simple. I haven't made my own Makefiles, because when I see them, they are complex and not easy to understand.

That's why I chose CMake for my projects. If this is simple, I chose the right build tool.

This build file of a large project is easier to understand than the "Simple Makefile"

https://gitlab.kitware.com/vtk/vtk/blob/master/CMakeLists.tx...

Post reply on HN