Millet Porridge

English version of https://corvo.myseu.cn

0%

Using Makefiles

How Makefiles Work

The way I see it, a Makefile lets you build your code easily without an IDE. But modern IDEs do so much work for us that we no longer understand how a program is compiled, linked and executed. For us, it’s quite necessary to understand how the IDE itself works.

VS? NO !!!

How exactly does VS compile, link and execute our code?

For the detailed process see How a compiler works

1
2
3
4
5
cl /EHsc /Femy.exe test.cpp 

# Another way
cl /EHsc /c test.cpp /Fomy.obj # compilation
link my.obj /OUT:my.exe # linking
1
2
3
4
5
6
7
8
9
10
11
12
#define UNICODE
#define _WIN32_WINNT 0x0A00
#include <afxwin.h>
#include "windows.h"

int WINAPI WinMain(HINSTANCE hIstance, HINSTANCE hPrevInstance,
LPSTR szCmdLine, int iCmdShow)
{
AfxMessageBox(_T("Hello world"));
//MessageBox(0, L"Hello MessageBox", L"My app:", MB_SETFOREGROUND);
return 0;
}

gcc aspires to be the standard compiler, so it makes sense for it to be open source. vim/emacs aspire to be the standard editors for programmers, so it makes sense for them to be open source too. apache aspires to be the standard web server platform, so it makes sense for it to be open source. android aspires to be the standard mobile internet platform, so it makes sense for it to be open source.

Makefile Basics

1
2
target... : requisites...
command

The tab before the command is required. For a Makefile, lines starting with a tab are commands.

What’s Clever About Makefiles

The first big advantage of Makefile:

  • After you change one file, it does not recompile the whole project.

  • It only compiles the changed files (isn’t that what IDEs do?)

  • If you see garbled output, use chcp 65001 to switch to utf-8 encoding

The second big advantage: People reading the source code can clearly see from the Makefile how the program is built and how a given file is produced. If you only look at all those .h and .cpp files in an IDE, even a genius would need a long time to read through them.

In a Makefile, long content can be wrapped with \. The rules only tell make two things: the file dependencies and how to generate the target file.

  • Generally, make executes commands with the standard UNIX shell, i.e. /bin/sh.

The third big advantage: The universal source-package installation greatly promoted the spread of open source software. Almost every piece of software around us carries traces of open source software, and the explosion of open source is inseparable from the popularity of makefile — it made programmers’ work so much simpler.

Using C++ Libraries

Please download the example program

SharedLibDynamicLink.tar.gz

$^: represents all the dependency files

$@: represents the target

$<: represents the first dependency file

A Slightly More Complex Example

MyDict.tar.gz

Finally, let me introduce the three-line love poem of Linux:

1
2
3
./configure 
make
make install
1
2
3
wget http://ftp.gnu.org/gnu/tar/tar-latest.tar.xz       # download the tar source package
xz -d tar-latest.tar.xz
tar -xvf tar-latest.tar

Some Simple Reference Documents

Makefile.pdf

CMake Practice.pdf

If you’re interested in LFS and already have some Linux basics, you can download the docs and give it a try. LFS downloads