使用Makefile编译main.cpp

参考文章:http://blog.csdn.net/haoel/article/details/2886

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

初学编写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;
}

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

效果如下:



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

题外话:

       程序编译过程一般为:预处理--->编译--->链接,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的好处,在大型工程就体现的很明显了,

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








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

friendan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值