美文网首页
Ubuntu下面Makefile的使用方法

Ubuntu下面Makefile的使用方法

作者: jianshuttl | 来源:发表于2016-11-04 16:16 被阅读961次
  1. 编译程序的命令
    g++ hello_world.cpp -o hello
    -o定义输出文件额名字为hello,该输出文件和源文件在同一个文件夹下面。

运行可执行文件hello

./hello

移除文件 rm hello

编译文件得到可执行文件的同时,保留产生的中间文件

g++ -save-temps hello_world.cpp -o hello

单个文件编译过程:
实际的编译过程:预处理,对应临时文件hello_world.ii

g++ -E hello_world.cpp -o preprocessed.ii

cat preprocessed.ii

预处理的工作主要包含去掉注释,然后将我们include的库tack上,以上过程使我们自己主动调用预处理器的过程

cat hello_world.ii

则展示了我们在第5)中编译时保留的中间文件,两者是一样的

实际的编译过程:这是真正的编译过程compilation step,对应临时文件hello_world.s(assembly code)

我们自己把.ii文件进行assembly code得到.s的方法。

g++ -S preprocessed.ii -o complied.s

.s文件是高级语言和机器语言之间的中间环节

实际的编译过程:Assembly,将.s对应的assembly code转换成.o对应的机器语言的过程,也叫machine-readable code或者object code

让编译器将.s文件assembly起来

g++ -c complied.s -o assembled.o

实际的编译过程:最后一步Linking,产生最终的可执行文件。

"Undefined reference" errors are pretty much always linking errors, and you will probably have them. Remember this.

我们通过连接一堆.o文件,得到.exe文件的过程,命令:

g++ assembled.o -o hello_manual

多个文件编译过程:
举例,如果我们有定义一个class,然后我们的文件中包含dog.hpp,dog.cpp和main.cpp三个文件,然后我们只使用以下两个命令:

g++ -c main.cpp -o main.o

g++ main.o dog_program

的话就会出错,告诉我们undefined reference of dog::bark()

因为对于不同的.cpp文件,都要生成一个object file,也就是.o文件。所以如果我们用以下命令:

g++ -c main.cpp -o main.o

g++ -c dog.cpp

g++ dog.o main.o -o dog_program

的话,就不会出错。

我们如果修改main.cpp中的内容的话,我们只需要重新用最后一个连接命令。但是,如果我们修改了dog class本身的内容的话,例如添加一个函数,我们就需要重新产生object file of dog.cpp,然后重新连接。

关于Make的介绍
用自己常用的记事本创建一个Makefile,并注意大小写。在program对应的目录下面。

gedit Makefile

Makefile里面语句的书写规则是

Target: tgt_dependency1 tgt_dependency2 ……
Command

所以dog.o和main.o对应的语句分别是:

dog.o: dog.hpp dog.cpp
g++ -c dog.cpp

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

在Makefile中Tab是很重要的,所以不要忘记在command对应的第二行加Tab

Makefile的编译顺序

如果Makefile中有如下语句

animal_assembly : moose goose cat
command

moose : antlers hooves fur
command

goose : beak wings webbed_feet interest_in_bread
command

cat : whiskers evil_personality
command

我们可以看到animal_assembly的dependency是 moose goose cat。如果文件夹中存在moose goose cat的话,make命令只需要执行第一句就可以了。如果文件夹中缺少moose goose cat中的一个或者几个,make命令执行的时候,需要先找到moose goose cat的生成方法,然后执行对应的命令,最后执行animal_assembly生成的命令。

moose : antlers hooves fur
command

animal_assembly : moose goose cat
command

goose : beak wings webbed_feet interest_in_bread
command

cat : whiskers evil_personality
command

如果Makefille是以上形式的话,我们只运行make的话,Makefile会只执行第一句,因为第一句的dependency都存在了,所以只把moose生成的命令执行完就好了。如果我们要生成animal_assembly,就要运行命令make animal_assembly。所以,我们需要把最重要的命令,我们最重要生成的object file对应的命令放在第一行。

所以我们的dog_program的完整的Makefile文件应该是:

dog_program: dog.o main.o
g++ dog.o main.o -o dog_program

dog.o: dog.hpp dog.cpp
g++ -c dog.cpp

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

在Makefile的最后写clean语句。

clean:
rm dog_program *.o

然后我们在命令窗口运行make clean的话,就会删除文件夹中生成的可执行文件,和所有过程中产生的副产品。

对于Makefile中的dependency list,我们需要将每个object file的dependency list都写好。因为make不负责检查文件中的具体的语句,只负责执行Makefile中的语句。

dog_program:
g++ dog.o main.o -o dog_program

dog.o: dog.hpp dog.cpp
g++ -c dog.cpp

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

如果像上面所示去掉dog_program的dependency list的话,运行make就会出错,因为main是依赖于dog.cpp的。

如果文件中本身就存在dog.o和main.o的话,运行make不会出错,因为make就是先check dog.o main.o是否存在,存在的话就直接运行。

所以,我们如果修改了main.cpp或者dog.cpp的话,我们需要重新生成dog.o和main.o。因为make是不管这个问题的。

make这个命令的功能就是执行Makefile中我们要求执行的语句,对结果没有任何的预期,也不会检查命令有没有问题。所以,我们必须遵守Makefile书写中的一些规则。

all : fill_file_with_nonsense
echo "I have mostly created a lot of junk today!"

fill_file_with_nonsense : create_file
echo "Hello, there is nothing important here" > silly_file

create_file :
touch silly_file touch是Unix中的典型命令,用于生成空的文件

move_file :
mv silly_file silly_file_new_name

delete_file :
rm _file

open_file :
gedit another_silly_file

clean :
touch junk1 junk2 junk3 junk4 junk5

really_clean :
rm junk*

如果想体验的更加清楚,就可以运行这个文件中的内容,然后就知道make完全不会管结果是什么,只是没有脑子的执行命令。

解释上面的内容:

Makefile的书写规则。all: 放在第一句,把所以我们要生成executable依赖的Targets列出来,这样我们需要的所有的文件都可以生成。我们看到all对应的dependency是file_file_with_nonsense,就去找file_file_with_nonsense的生成语句,发现它的dependency是create_file,然后去找create_file的生成语句,就到touch silly_file,touch是Unix中的典型命令,用于生成空的文件。create_file的语句执行完之后,回到file_file_with_nonsense,执行echo "Hello, there is nothing important here" > silly_file,就把"Hello, there is nothing important here" 写入silly_file中,file_file_with_nonsense的语句也执行完之后,我们就返回到all,然后在命令行输出"I have mostly created a lot of junk today!"。

因为其他的target,不在all的dependency list中,也不在all的dependency的dependency当中,所以只能通过make target的形式来调用对应的命令。

Marvelous macros(宏)
一个宏的示例,宏就是Makefile中的variables,用来定义我们需要的操作,一些变量之类的

CXX = clang++

FLAGS = -O

hello : hello_world.cpp
$(CXX) $(FLAGS) $? -o $@

clean :
rm hello

CXX,这是一个预定义的宏,default value是g++。这里把CXX定义成clang++了。

FLAGS,这里定义的是-O。FLAGS也不一定非得定义成-o,也可以是some moose have large antlers,但是这样定义的话,就会导致调用的时候出错。

对于上面的文件,我们在命令行输入make的时候,实际运行的是clang++ -O hello_world.cpp -o hello。

如果我们把CXX=clang++这一行删掉的话,在命令行输入make,实际运行的就是g++ -O hello_world.cpp -o hello。

定义好macro宏,我们使用的时候,就要用$(MACRO)这样的形式,这是makefile语言的一种语法。我们注意到MACRO全部用的大写,虽然不是明确规定的,但是通常情况下用大写。

$?和$@是makefile language里面特别的预定义的宏。$?是指的"names of the dependencies(newer than the target)",$@是指的"name of the target"。

Complier and liner flags in CS 225

This defines our compiler and linker, as we've seen before.

CXX = clang++ LD = clang++

These are the options we pass to the compiler.

-std=c++1y means we want to use the C++14 standard (called 1y in this version of Clang).

-stdlib=libc++ specifies that we want to use the standard library implementation called libc++

-c specifies making an object file, as you saw before

-g specifies that we want to include "debugging symbols" which allows us to use a debugging program.

-O0 specifies to do no optimizations on our code.

-Wall, -Wextra, and -pedantic tells the compiler to look out for common problems with our code. -Werror makes it so that these warnings stop compilation.

CXXFLAGS = -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -Werror -pedantic

These are the options we pass to the linker.

The first two are the same as the compiler flags.

-l<something> tells the linker to go look in the system for pre-installed object files to link with.

Here we want to link with the object files from libpng (since we use it in our code) and libc++. Remember libc++ is the standard library implementation.

LDFLAGS = -std=c++1y -stdlib=libc++ -lpng -lc++abi

相关文章

  • Ubuntu下面Makefile的使用方法

    编译程序的命令g++ hello_world.cpp -o hello-o定义输出文件额名字为hello,该输出文...

  • 【编译】makefile使用

    本文重在记录makefile的使用方法,不介绍相关原理。注意:所有的makefile指令前面都需要两个 tab 缩...

  • makefile入门一

    Linux下Makefile详解 下面就来看如何写Makefile文件:Makefile文件编写规范: 下来看事例...

  • Makefile

    本周学习了Makefile的使用方法,在我个人看来这并不是一什么崭新的知识,Makefile实质上只是将编译...

  • makefile 入门基础知识

    一.makefile格式 1.makefile 文件一般命名 为makefile 即可,放在目录下面,直接调出 t...

  • YOLO v3训练自己的数据集(GPU)

    环境:Ubuntu 16.04 + CUDA8.0 + cudnn5.1 下载模型: 修改Makefile文件配置...

  • linux中的Makefile

    时间:2018-02-26 作者:魏文应 Makefile 的变量 下面是定义makefile: 然后引用变量:...

  • openwrt生成apk

    在建立文件夹和文件 编辑hello_world.c 在src下面编辑Makefile , vim Makefile...

  • C之make,Makefile

    跟我一起写 Makefile(一) - CSDN博客,结合YOLOv3下面的Makefile讲解看 makefil...

  • Ubuntu gmake 命令找不到

    今天, 需要make一些代码的时候,在Ubuntu12.10下提示gmake 找不到,于是我打开makefile ...

网友评论

      本文标题:Ubuntu下面Makefile的使用方法

      本文链接:https://www.haomeiwen.com/subject/vdkwuttx.html