[unix] ELI5 'makefile'

Solved

Hello, all! I'm relatively new to programming. Took a Intro to C and a Intro to C++ course and want to improve some of my knowledge, so I thought I'd do a project I found online. I stumbled upon using a 'makefile' on the project and decided to look into it.

I've looked at these sites and don't quite grasp how makefiles work or mostly, how they are created.

What exactly are targets? What are exactly are dependencies?

Say I have three files, main.cpp, hello.cpp, name.cpp and that name.cpp has a function that tells me the name, hello.cpp displays "hello, name::function" or whatever and main calls it all. (That was probably a quite terrible example), could I make a 'makefile' called swaggity_swag ? How would that look like?

ELI5 would be nice. thanks in advance.

Posted Mar 31, 2015, 12:32 PM UTC

13
nutrecht•

makefile

It's just a bunch of instructions to build an application. This saves you from typing all the commands every time you want to build your application from source. That's pretty much all there's to it: it saves (a lot of) time.

how they are created.

In a text editor. They're just text files.

What exactly are targets?

They're basically just tasks. The compilation of C++ into an executable consists of a bunch of steps. You combine those steps into a 'build'.

What are exactly are dependencies?

Dependencies are anything that your code depends on to be able to be built. For example if you want to create an MP3 player you might depend on an MP3 library to handle the decompression for you.

could I make a 'makefile' called swaggity_swag ?

You could but normally the filename is in fact 'Makefile'. If you type 'make' it will find the makefile and run the commands in there to build the project.

How would that look like?

There's plenty simple examples of what a makefile looks like.

2
exoticmatter•

The best documentation for make (and one of the best manuals for anything) is that for GNU make - anyone interested in using make should read it.

2
matt_hammond•

In bigger projects usually you have more than one .cpp file, for example:

user.cpp
network.cpp
camera.cpp

When you're building the program, you have to compile all the files separately. The output of this compilation are object files (.o) which you then link together to make an executable. You could do this by executing a compile command for each of the file and linking manually in the end, but that's a long and boring process so makefiles were invented.

A makefile is a file containing all these compilation and linking commands.

So when you run "make" in an Unix environment, it checks for the file named "makefile" for these commands and runs them for you.

So imagine now if you have a program with 20 files that you compiled, but you change one line in one file. You wouldn't want to recompile the whole program, that can take a lot of time... You can just recompile the one file you changed and link the object files together. Turns out makefiles can help you with that too.

You just have to tell the make that the exe file is created with the object files. If one object file is newer than the exe that means it has to be recompiled.

Well that's the basic idea... You can read about it the numerous makefile tutorials online.

1