1

I have got my first project for this semester and I have been asked to submit it with a makefile. The literature available on the internet is a bit overwhelming and combined with my laziness, I came to stackoverflow for simple answers. I have found this answer by Brendan Long as a good place to start with.

The example he gives is:

all: a3driver.o
g++ -o a3driver a3driver.o

a3driver.o: a3driver.cpp
g++ -c a3driver.cpp

which i understand. This looks exactly like the make files I have seen on a Unix system and which i used to compile c++ files(only used, did not need to understand).

Then i search further and an answer to this question suggests using CMake which is completely different from the code I have pasted above.

So my question at this stage is which direction should i take? Should I learn about the CMake or the GNUMake? I only intend to work on C++ files for now.

Community
  • 1
  • 1
detraveller
  • 285
  • 3
  • 17

2 Answers2

1

If you need a really really dead simple makefile for compiling one or few files only, then you are done with:

compile:
        g++ myprogram.cpp -o myprogram

(note that lines must be indented with tab, not spaces).

If you need flexibility, you are on the right path with CMake. I suggest you to explore CMake, starting from their good tutorial or a simple example -- as the basics are simpler to undestand from code rather than learn from manual.

My personal opionion is to avoid GNU Automake (colloquially known as Autohell) because of the unusual, verbose syntax that sometimes scares beginners and tricks more experienced users.

EDIT: CMake is not used to compile, rather, it can generate makefiles for you, starting from a synthetic description of the project (where are the files to be compiled? What libraries are required? etc.). And it does this by checking for libraries, identifying compiler and carrying out other sanity check you would need to code by yourself otherwise.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
1

Only you can answer this question because it depends heavily on your needs. Cmake is a "build control file generator", not a build control program. It doesn't actually build code: instead it will create a makefile, or a Visual Studio / Xcode / Eclipse project file, etc. You then use that build program (make, Visual Studio, XCode, Eclipse) to actually build the code.

Cmake is good if you need to support all those different types of builds across all those different architectures using their native build environments. If you're happy to use make on whichever architecture you need to build on (GNU make runs on all of those as well and all those IDEs except possibly Visual Studio have good integration with native make) then using make directly is fine. GNU make has lots of advanced features which make it very flexible.

I don't really agree with esseks assessment of the autotools although I know it's a very common opinion. Also note that automake itself does not use unusual, verbose syntax: automake files are just makefiles. However they have to be processed, and autoconf is how that's done... autoconf is more obscure although not as bad as people make it out to be, depending on your needs. This isn't the place for that discussion however.

I personally find cmake format even more annoying and strange than autotools, and it doesn't meet my needs in many ways (for example it's support for cross-compilation is not as good as autotools'). However I have to say its ability to generate native project files is really excellent--if you need it.

MadScientist
  • 92,819
  • 9
  • 109
  • 136