---------------------------------------------------------------------------------------------

初学编写Makefile,权当是笔记,希望对别人有些帮助吧!

我的目录结构如下:

使用Makefile编译main.cpp_Makefile

很简单吧,编译后的程序放在release目录。

----------------------------------------------------------------------------

该例子Makefile编写如下:


exe=./release/main.out  ### 定义宏(变量)

### 链接目标文件xxx.o,从而生成可执行文件
$(exe): main.o
gcc -o $(exe) -lstdc++ -Xlinker main.o

### 编译源文件xxx.cpp,生成目标文件xxx.o
main.o: main.cpp
gcc -lstdc++ -c main.cpp

.PHONY:clean ### 说明clean是个伪目标文件
clean:
rm *.out *.o


-----------------------------------------------------------------------------------------

main.cpp内容如下:


#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}


-----------------------------------------------------------------------------------------

效果如下:

使用Makefile编译main.cpp_g++_02


---------------------------------------------------------------------------------------------------------------

题外话:

       程序编译过程一般为:预处理--->编译--->链接,gcc有相应的参数对应这几个过程

查看gcc的帮助就一清二楚了:

gcc --help

Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  (Use '-v --help' to display command line options of sub-processes)
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler    // 汇编
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor  // 预处理
  -Xlinker <arg>           Pass <arg> on to the linker      //  链接
  -combine                 Pass multiple source files to compiler at once  // 同时编译多个源文件
  -save-temps              Do not delete intermediate files
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           for headers and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -b <machine>             Run gcc for target <machine>, if installed
  -V <version>             Run gcc version number <version>, if installed
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link  // 只预处理,不编译、不汇编、不链接
  -S                       Compile only; do not assemble or link    // 只编译,不汇编、不链接
  -c                       Compile and assemble, but do not link    // 编译和汇编,不链接
  -o <file>                Place the output into <file>
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension


Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by gcc.  In order to pass
 other options on to these processes the -W<letter> options must be used.


For bug reporting instructions, please see:
<URL:http://bugzilla.redhat.com/bugzilla>.

---------------------------------------------------------------------------------------------------------------------

以上例子,如果不用Makefile,使用gcc,敲以下一条命令就可以了:

gcc -lstdc++ main.cpp

-----------------------------------------------------------------------------------------------------------------

Makefile的好处,在大型工程就体现的很明显了,

小工程我们敲几条命令感觉不到什么,慢慢体验吧。。。