0

^ This is not a duplicate question ^

Updated:

I'm fairly new to programming and I've only now just come across makefiles.

I've downloaded a bunch of tutorials and source code from a variety of places over the last few months; until now I've only been using VS solutions and been avoiding them.

I've seen multiple methods of using Makefiles on google, but they all differ in complexity...

I don't have a clue about them or how to use them. Thanks.

Edit (re-worded):

To summarise, due to the obvious confusion, my questions are (and always were):

1) Is there a good tutorial that comes recommended or is it easy to get started with Makefiles?
2) How do you launch and run an existing Makefile? And, 
3) Do Makefiles hold any vital data about the program or could I just use the `.h` and `.cpp` files that come with them in a new solution built from scratch?
Reanimation
  • 3,151
  • 10
  • 50
  • 88
  • 1
    possible duplicate of [How to run makefile in Windows](http://stackoverflow.com/questions/17158419/how-to-run-makefile-in-windows) – cbr Nov 22 '13 at 12:42
  • A better question would be: Why would you *want* to use raw Makefiles? Is that a specific requirement of yours? – thokra Nov 22 '13 at 12:47
  • I'm sorry if it's a duplicate. I did do a search first but found nothing relevant. Um... Ok, It's just some tutorials and examples come with Makefiles rather than a Solution, so I was wondering how to use them so I could run them. Thanks for posting. – Reanimation Nov 22 '13 at 12:49
  • @Reanimation: If they are cross-platform projects that's somewhat understandable. If they're Windows-only projects then I don't get what significant advantage the authors get with Makefiles compared to VS projects and solutions. Can you name an example? – thokra Nov 22 '13 at 12:51
  • 1
    Note that in many beginner cases, you don't need to write any Makefiles at all: GNU make or nmake (comes with Visual Studio) have default rules. So if you have an 'a.cpp' file in your current directory, try running `make a` or `nmake a.exe` or similiar to build it. – Frerich Raabe Nov 22 '13 at 12:51
  • 1
    @thokra: A big advantage of Makefiles: you get to learn about the toolchain which Visual Studio project files tend to hide. – Frerich Raabe Nov 22 '13 at 12:52
  • @FrerichRaabe: Sure, but you can just as well look inside the XML VS generated and you get a lot of those details as well. I'm a Linux guy and use `make` almost on a daily basis. But sincerely, if you got something like `ninja` ... especially in conjunction with CMake. To be fair though, learning CMake isn't that much fun either. – thokra Nov 22 '13 at 12:55
  • @thokra I have no idea of an advantage. That's why I've decided to try and use them for the first time. It's surprising how many people include them in source code. I'd rather use VS Solutions personally but sometimes I find really good examples only to find I can't use them as I don't know how to. – Reanimation Nov 22 '13 at 12:55
  • Also, can we unlink that "windows" question? I've just read it and it doesn't answer my two questions, it suggests a collection of GNU utilities, not using existing makefiles or how to get started. It's irrelevant. I'd like to hear peoples answers, not have them assume it already has one... Thanks. – Reanimation Nov 22 '13 at 13:00
  • 1
    Ahh, I forgot. Some people writing stuff for Linux and other UNIX derivatives propose compilation of their `Makefile` project using MSYS, which is part of MinGW - Cygwin is also an alternative. They let you compile all the stuff unaltered, i.e. you install MSYS and MinGW, open an MSYS console, and basically configure and compile the project like you would on Linux. I always hated that ... – thokra Nov 22 '13 at 13:05
  • possible duplicate of [How to make SIMPLE C++ Makefile?](http://stackoverflow.com/questions/2481269/how-to-make-simple-c-makefile) – devnull Nov 22 '13 at 14:11
  • I don't want to know how to make them... I want to know how to use them assuming you have been provided some. – Reanimation Nov 23 '13 at 02:10
  • I don't really understand the "do `Makefile`s hold any vital data about the program" question: more precisely, your formulation is strange and probably wrong. `Makefile`-s describe the build process. – Basile Starynkevitch Nov 23 '13 at 07:09
  • It's a simply question, which I guess should be answered "no" based on what you're saying. I have an example with header files, classes and makefiles. I was concerned that the makefiles contained something vital to using the program or if I would be able to just import the `.h`. and `.cpp` into a new solution and discard the makefiles. I think having no experience with or knowledge about them (as posted in my question) can justify my naivety. I'm trying to find out how they can be used/launched/run. – Reanimation Nov 23 '13 at 18:40

3 Answers3

10

The question of when to use a Makefile is a matter of opinion. Certainly you can use some alternative builder (like e.g. omake or ninja, but there are many others). You certainly want some building system as soon as you have more than one translation unit in your software. If using make the Makefile describes the build process by detailing dependencies. Often, building you program is just done thru the simple make command.

Read more about C++ programming, and some C++ reference, for the terminology (perhaps also the C++11 standard n3337). See also this draft report.

Some utilities, e.g. cmake, qmake, automake, etc... are Makefile generators. They generate a complex Makefile which is usually not using all the features of GNU make.

If you decide to use make, I recommend using GNU make (this is also an opinion). In that case, read carefully its documentation, which I find quite readable and which contains a good tutorial. Don't forget to use a recent version: the version 4.0 of GNU make was released in october 2013 and brings a lot of interesting new features (plugins, and most importantly extensibility thru guile) whih are interesting for complex builds.

To debug complex Makefile-s consider using remake (notably as remake -x).

In practice, GNU make has a lot of built-in rules. You get them with make -p; so use them.

You may also choose to code for the POSIX make utility (which is a standard specification, but less powerful than GNU make, which can be configured to follow the POSIX spec).

How to code a Makefile is a matter of taste, and of software complexity. If you have 2 or 3 *.cc files with a single .h common header, you don't want all the fancy tricks (e.g. automatic dependencies, as provided by GCC and make) that are worth using on a software of many hundreds source files (and sometimes C++ source code is generated, e.g. by ANTLR, or by GNU bison, or by SWIG, by Qt moc, or by your own metaprogramming script, so the build system has to know that; for an example, see also RefPerSys).

Here is a tiny GNU Makefile to help you:

## file Makefile
CXX= g++
CXXSOURCES= main.cc other.cc
CXXOBJECTS= $(patsubst %.cc, %.o, $(CXXSOURCES))
OPTIMFLAGS= -g -O
PACKAGES= glib-2.0
PKGCONFIG= pkg-config
CXXFLAGS= -std=c++11 -Wall $(OPTIMFLAGS)
CPPFLAGS:=  $(shell $(PKGCONFIG) --cflags $(PACKAGES))
LIBES:= $(shell $(PKGCONFIG) --libs $(PACKAGES)) -ldl
.PHONY: all clean

all: myprog
myprog: $(CXXOBJECTS)
    $(LINK.cc) -rdynamic $^ $(LIBES) -o $@

$(CXXOBJECTS): yourheader.h

clean:
    $(RM) *.o *.so *.orig *~ myprog core* *.hh.gch
## eof Makefile

I don't claim it is a perfect example (it is not). Be careful, tabulations are significant for make and I am not able to type them above. You'll use make all (or just make) to build your program, and make clean to remove non-source files.

For obscure reasons, sometimes Makefile may be generated using autotools or cmake. For simple programs, it might not worth the effort to learn how to use them.

There are some few cases when GNU make is not worthwhile or appropriate: in MELT I nearly gave up using it (by calling some autogen generated shell script from it) because it is a bootstrapped language where a set of C++ files is generating itself from a set of MELT files, so you have a multiple to multiple quasi-circular dependency relation. Probably using a GUILE extension could have helped a lot. But such nightmare situations are rare.

PS: I answered as a Linux user; don't know what applies -or not- to Windows which I never use. See also these hints about C or C++ programming on Linux.

PS2: Paul Evans' answer is relevant: don't write your own Makefile, copy some existing one to suit your needs.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

If you are asking for a good tutorial then I think this one is good! You can totally follow it. I learned the make file by it.

Archeosudoerus
  • 1,101
  • 9
  • 24
1

The fundamental concept for success when starting to roll your own makefile's is don't! Always grab a similar, simple, working makefile as a template and alter it to your own build. Only after getting this working, you may look at ways to improve it, make it more generic, etc through diligent reading and study. Then one day may come in the far, far future where you can actually write a makefile from scratch. But don't be disappointed if that day never comes.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Positive response there. I never thought I'd get this far with programming, so I won't write it off just yet. Thanks for posting. – Reanimation Nov 22 '13 at 12:52
  • Makefiles are simple and let you easily script your compilation process for all sorts of things. It's perfectly feasible to learn to write makefiles from the manpage in a short time -- much, much easier than C++! – Nicholas Wilson Nov 22 '13 at 12:52
  • You should simply use VS's own build-system. Makefiles - albeit powerful - are simply arcane, with horrible syntax and take a lot fo effort to understand fully. It's simply not worth it. – thokra Nov 22 '13 at 12:52
  • 1
    I agree with Paul, but I'd consider this a comment, not an answer. It's an opinion, nothing more. – stefan Nov 22 '13 at 12:53
  • It's not the first time I read advice like this (don't roll your own from zero), and I agree. Though of course that can account for a lot of cruft I have seen, and we all know what happens with _technical debt_… – hmijail Aug 27 '15 at 10:15