当你在安装PostgreSQL软件时,执行以下代码你知道实际上意味着什么吗?
./configure
make
make install
别急,读完这篇文章你就自然会明白了。
PostgreSQL是用C编写的,因此安装PostgreSQL软件其本质上不过是编译和运行源代码中的所有C程序。
我们先来看看下面这个例子。
1、如何在Linux中编译和运行C程序?
1、首先创建一个名为first.c的程序
[root@ison2 cmi]# cat first.c
#include<stdio.h>
int main(void)
{
printf("Hello! This is a test prgoram.\n");
}
2、接着我们来编译它:
[root@ison2 cmi]# gcc first.c -o first.o
[root@ison2 cmi]#
上面的命令将编译first.c程序并创建一个名为first.o的可执行文件。
3、然后我们执行./first.o来运行
[root@ison2 cmi]# ls -lrt
total 16
-rwxr-xr-x 1 root root 35 Mar 26 22:51 configure
-rw-r–r-- 1 root root 83 Mar 26 22:54 first.c
-rwxr-xr-x 1 root root 6449 Mar 26 22:58 first.o
[root@ison2 cmi]#
[root@ison2 cmi]# ./first.o
Hello! This is a test prgoram.
我们还可以使用make和makefile以及make install执行相同的程序。
2、如何使用Make和Make install运行C程序
Make是通用的工作流程程序,通常用于编译。除此之外它还可以用于任何东西。
当你执行诸如“ make”之类的操作时,默认情况下,make程序将从当前目录中名为“ Makefile”的文件中执行名为“ all”的规则。该规则通常调用编译器将一些源代码编译为二进制文件。
Makefile语法:
all:
{
your compile code goes here}
install:
{
your install code goes here}
make将打开Makefile文件并执行其中的"all"后面的部分。即它的作用等同于前面的“ gcc first.c -o first.o”。
当你执行“ make install”时,make程序将从上一步获取二进制文件并将其复制到某些适当的位置,以便可以对其进行访问。
make install将打开Makefile文件并在其中执行"install"后面的部分,即它的作用等同于前面的“ ./first.o”的。
2.1、让我们来看一个如何使用makefile的例子
1、使用“all”和“install”字段来创建一个Makefile。
[root@ison2 cmi]# cat Makefile
all:
gcc first.c -o first.o
install:
./first.o
我刚刚在vi编辑器中使用all:和install:字段创建了一个Makefile,并添加了我之前使用的命令。
现在,进行安装。
2、当您运行make时,它将进入Makefile文件并运行该文件的“all”的部分。
[root@ison2 cmi]# make
gcc first.c -o first.o
[root@ison2 cmi]#
3、运行make install来执行上面make生成的first.o文件
[root@ison2 cmi]# make install
./first.o
Hello! This is a test prgoram.
另外,如果你想让你的Makefile中显示消息,那么可以使用@echo来做到这一点。
修改Makefile:
[root@ison2 cmi]# cat Makefile
all:
gcc first.c -o first.o
@echo All PostgreSQL has successfully made
install:
./first.o
运行make:
[root@ison2 cmi]# make
gcc first.c -o first.o
All PostgreSQL has successfully made
[root@ison2 cmi]#
现在,我们了解了如何在Linux中运行程序。我们可以使用“ ./ProgramName”来实现
如果你有大量的C程序和许多制作规则,那么使用Make和Makefile确实会很有帮助。
编写并运行一个名为configure的简单shell脚本。
[root@ison2 cmi]# cat configure
echo "I am checking configuration"
[root@ison2 cmi]# ./configure
I am checking configuration
[root@ison2 cmi]#
2.2、那么以下内容在PostgreSQL中做了什么?
说了这么多,让我们回到文章开头的问题,在PostgreSQL中执行下面这个三个命令时究竟发生了什么?
./configure
make
make install
- configure:所有的依赖项检查都是在configure中编写的,你可以使用vi编辑器去看看configure文件,并尝试读取它(如果可能)。
- make:你将在软件目录中拥有一个makefile,该文件会将您重定向到拥有所有程序的src目录,你的make将在configure选项中选择的位置中指定的位置编译所有程序。
- make install:运行它们。
所以,这也就是为什么需要在Linux机器上安装GNU Make 3.80或更高版本以及GCC来安装PostgreSQL的原因。