1
2
3
4
5
6
|
[root@xpleaf ~]# vim hello.c
#
include
<stdio.h>
int
main(
void
)
{
printf(
"Hello World\n"
);
}
|
1
2
3
4
|
[root@xpleaf ~]# gcc -c hello.c
[root@xpleaf ~]# ll hello*
-rw-r--r--
1
root root
64
Aug
16
03
:
57
hello.c
-rw-r--r--
1
root root
1496
Aug
16
04
:
21
hello.o
|
1
2
3
4
5
|
[root@xpleaf ~]# gcc -o hello hello.o ===>指定二进制文件名为hello
[root@xpleaf ~]# ll hello*
-rwxr-xr-x
1
root root
6465
Aug
16
04
:
23
hello
-rw-r--r--
1
root root
64
Aug
16
03
:
57
hello.c
-rw-r--r--
1
root root
1496
Aug
16
04
:
21
hello.o
|
1
2
|
[root@xpleaf ~]# ./hello
Hello World
|
1
2
3
4
|
[root@xpleaf ~]# gcc hello.c
[root@xpleaf ~]# ll hello.c a.out
-rwxr-xr-x
1
root root
6465
Aug
16
04
:
30
a.out ===>默认生成的二进制文件
-rw-r--r--
1
root root
64
Aug
16
03
:
57
hello.c
|
1
2
|
[root@xpleaf ~]# ./a.out
Hello World
|
1
2
3
4
5
6
7
|
[root@xpleaf ~]# vim thanks.c
#
include
<stdio.h>
int
main(
void
)
{
printf(
"Hello World\n"
);
thanks_2();
}
|
1
2
3
4
5
6
|
[root@xpleaf ~]# vim thanks_2.c
#
include
<stdio.h>
void
thanks_2(
void
)
{
printf(
"Thank you!\n"
);
}
|
1
2
3
4
5
6
|
[root@xpleaf ~]# gcc -c thanks.c thanks_2.c
[root@xpleaf ~]# ll thanks*
-rw-r--r--
1
root root
74
Aug
16
04
:
38
thanks_2.c
-rw-r--r--
1
root root
1504
Aug
16
04
:
38
thanks_2.o
-rw-r--r--
1
root root
90
Aug
16
04
:
37
thanks.c
-rw-r--r--
1
root root
1560
Aug
16
04
:
38
thanks.o
|
1
2
3
|
[root@xpleaf ~]# gcc -o thanks thanks.o thanks_2.o
[root@xpleaf ~]# ll thanks
-rwxr-xr-x
1
root root
6606
Aug
16
04
:
39
thanks
|
1
2
3
|
[root@xpleaf ~]# ./thanks
Hello World
Thank you!
|
1
2
3
4
5
6
7
8
9
|
[root@xpleaf ~]# vim sin.c
#
include
<stdio.h>
int
main(
void
)
{
float value;
value = sin (
3.14
/
2
);
printf(
"%f\n"
,value);
}
|
1
2
3
4
5
6
7
|
[root@xpleaf ~]# gcc -c sin.c
sin.c: In
function
ain
sin.c:
5
: warning: incompatible implicit declaration of built-
in
function
in
===>虽然有警告信息,但是仍然可以通过编译
[root@xpleaf ~]# ll sin*
-rw-r--r--
1
root root
101
Aug
16
06
:
19
sin.c
-rw-r--r--
1
root root
1512
Aug
16
06
:
20
sin.o
|
1
2
3
4
5
|
[root@xpleaf ~]# gcc -o sin sin.o
[root@xpleaf ~]# ll sin*
-rwxr-xr-x
1
root root
6473
Aug
16
06
:
22
sin
-rw-r--r--
1
root root
101
Aug
16
06
:
19
sin.c
-rw-r--r--
1
root root
1512
Aug
16
06
:
20
sin.o
|
1
2
|
[root@xpleaf ~]# ./sin
1.000000
|
1
2
3
4
|
main.c 主程序
hello.c 输出欢迎与提示信息
square.c 计算用户输入的数字的平方
cube.c 计算用户输入的数字的立方
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@xpleaf ~]# vim main.c
#
include
<stdio.h>
int
main(
void
)
{
int
num;
hello();
scanf(
"%d"
,&num);
square(num);
cube(num);
return
0
;
}
|
1
2
3
4
5
6
7
8
9
|
[root@xpleaf ~]# vim hello.c
#
include
<stdio.h>
void
hello(
void
)
{
printf(
"Welcome to my procedure!\n"
);
printf(
"This procedure will get the number you input.\n"
);
printf(
"And then output the square and cube of the number.\n"
);
printf(
"Now,please input a number:"
);
}
|
1
2
3
4
5
6
|
[root@xpleaf ~]# vim square.c
#
include
<stdio.h>
void
square(
int
n)
{
printf(
"The square of %d is %d.\n"
,n,n*n);
}
|
1
2
3
4
5
6
|
[root@xpleaf ~]# vim cube.c
#
include
<stdio.h>
void
cube(
int
m)
{
printf(
"The cube of %d is %d.\n"
,m,m*m*m);
}
|
1
2
3
4
|
[root@xpleaf ~]# gcc -c main.c
[root@xpleaf ~]# gcc -c hello.c
[root@xpleaf ~]# gcc -c square.c
[root@xpleaf ~]# gcc -c cube.c
|
1
2
3
|
[root@xpleaf ~]# gcc -o main main.o hello.o square.o cube.o
[root@xpleaf ~]# ll main
-rwxr-xr-x
1
root root
7472
Aug
16
07
:
02
main
|
1
2
3
4
5
6
7
|
[root@xpleaf ~]# ./main
Welcome to my procedure!
This procedure will
get
the number you input.
And then output the square and cube of the number.
Now,please input a number:
3
The square of
3
is
9
.
The cube of
3
is
27
.
|
1
2
3
|
[root@xpleaf ~]# vim makefile
main: main.o hello.o square.o cube.o
gcc -o main main.o hello.o square.o cube.o ===>这一行开始必须是tab键补全
|
1
2
3
4
5
6
7
8
|
[root@xpleaf ~]# make
cc -c -o main.o main.c
cc -c -o hello.o hello.c
cc -c -o square.o square.c
cc -c -o cube.o cube.c
gcc -o main main.o hello.o square.o cube.o
[root@xpleaf ~]# ll main
-rwxr-xr-x
1
root root
7472
Aug
16
07
:
12
main
|
1
2
3
4
5
6
7
|
[root@xpleaf ~]# ./main
Welcome to my procedure!
This procedure will
get
the number you input.
And then output the square and cube of the number.
Now,please input a number:
2
The square of
2
is
4
.
The cube of
2
is
8
.
|
1
2
|
目标(target):目标文件
1
目标文件
2
<tab> gcc -o 欲新建的可执行文件 目标文件
1
目标文件
2
|
1
2
3
4
5
|
[root@xpleaf ~]# vim makefile
main: main.o hello.o square.o cube.o
gcc -o main main.o hello.o square.o cube.o
clean:
rm -f main main.o hello.o square.o cube.o
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@xpleaf ~]# ll *.o main
-rw-r--r--
1
root root
1544
Aug
16
22
:
44
cube.o
-rw-r--r--
1
root root
1856
Aug
16
22
:
44
hello.o
-rwxr-xr-x
1
root root
7472
Aug
16
22
:
44
main
-rw-r--r--
1
root root
1720
Aug
16
22
:
44
main.o
-rw-r--r--
1
root root
1536
Aug
16
22
:
44
square.o
[root@xpleaf ~]# make clean
rm -f main main.o hello.o square.o cube.o
[root@xpleaf ~]# ll *.o main
ls: cannot access *.o: No such file or directory
ls: cannot access main: No such file or directory
|
1
2
3
4
5
6
|
[root@xpleaf ~]# vim makefile
OBJS = main.o hello.o square.o cube.o
main: ${OBJS}
gcc -o main ${OBJS}
clean:
rm -f main ${OBJS}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@xpleaf ~]# CFLAGS=
"-Wall"
make clean main
rm -f main main.o hello.o square.o cube.o
cc -Wall -c -o main.o main.c
main.c: In
function
ain
main.c:
5
: warning: implicit declaration of
function
ello
main.c:
7
: warning: implicit declaration of
function
quare
main.c:
8
: warning: implicit declaration of
function
ube
cc -Wall -c -o hello.o hello.c
cc -Wall -c -o square.o square.c
cc -Wall -c -o cube.o cube.c
gcc -o main main.o hello.o square.o cube.o
[root@xpleaf ~]# ll main
-rwxr-xr-x
1
root root
7472
Aug
16
23
:
20
main
===>相当于在编译时使用了-Wall参数,输出警告信息
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@xpleaf ~]# vim makefile
OBJS = main.o hello.o square.o cube.o
CFLAGS = -Wall
main: ${OBJS}
gcc -o main ${OBJS}
clean:
rm -f main ${OBJS}
[root@xpleaf ~]# make clean main
rm -f main main.o hello.o square.o cube.o
cc -Wall -c -o main.o main.c
main.c: In
function
ain
main.c:
5
: warning: implicit declaration of
function
ello
main.c:
7
: warning: implicit declaration of
function
quare
main.c:
8
: warning: implicit declaration of
function
ube
cc -Wall -c -o hello.o hello.c
cc -Wall -c -o square.o square.c
cc -Wall -c -o cube.o cube.c
gcc -o main main.o hello.o square.o cube.o
[root@xpleaf ~]# ll main
-rwxr-xr-x
1
root root
7472
Aug
16
23
:
24
main
===>实现d一样的功能
|
1
2
3
4
5
6
|
[root@xpleaf ~]# vim makefile
OBJS = main.o hello.o square.o cube.o
main: ${OBJS}
gcc -o $@ ${OBJS} ===>$@即代表这里的目标main
clean:
rm -f main ${OBJS}
|
步骤 | 目的 | 过程 | 注意 |
1 | 获得源文件 | 将源文件在/usr/local/src目录下解压缩 | 建议此目录下 |
2 | 获得安装流程 | 阅读新建立目录下的INSTALL与README文件 | |
3 | 依赖软件安装 | 根据b的内容,安装其它需求的软件(非必须) | |
4 | 建立makefile | 运行自动检测程序configure或config,建立makefile文件 | 注意指定安装目录 |
5 | 编译 | 根据makefile的内容信息,执行make命令编译 | |
6 | 安装 | 执行make中的install目标,安装软件到d步骤指定的目录中 |
1
2
3
4
5
6
7
|
[root@xpleaf ~]# ll ntp-
4.2
.4p7.tar.gz
-rw-r--r--
1
root root
3382146
Aug
19
2015
ntp-
4.2
.4p7.tar.gz
[root@xpleaf ~]# cd /usr/local/src/ ===>切换目录
[root@xpleaf src]# tar -xvf /root/ntp-
4.2
.4p7.tar.gz
……
[root@xpleaf src]# ll -ld ntp-
4.2
.4p7/
drwxrwsrwx
24
427
6011
4096
Aug
17
01
:
52
ntp-
4.2
.4p7/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[root@xpleaf src]# cd ntp-
4.2
.4p7/
[root@xpleaf ntp-
4.2
.4p7]# vim INSTALL
……
The simplest way to compile
this
package
is
:
1
. `cd
' to the directory containing the package'
s source code and type
`./configure
' to configure the package for your system. If you'
re
using `csh' on an old version of System V, you might need to type
`sh ./configure
' instead to prevent `csh'
from trying to execute
`configure' itself.
Running `configure' takes a
while
. While running, it prints some
messages telling which features it
is
checking
for
.
2
. Type `make' to compile the
package
.
3
. Optionally, type `make check' to run any self-tests that come
with
the
package
.
4
. Type `make install' to install the programs and any data files and
documentation.
5
. You can remove the program binaries and object files from the
source code directory by typing `make clean'. To also remove the
files that `configure' created (so you can compile the
package
for
a different kind of computer), type `make distclean'. There
is
also a `make maintainer-clean' target, but that
is
intended mainly
for
the
package
's developers. If you
use
it, you may have to
get
all sorts of other programs
in
order to regenerate files that came
with
the distribution.
……
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@xpleaf ntp-
4.2
.4p7]# ./configure --help
……
Installation directories:
--prefix=PREFIX install architecture-independent files
in
PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files
in
EPREFIX
[PREFIX]
By
default
, `make install' will install all the files
in
`/usr/local/bin
', `/usr/local/lib'
etc. You can specify
an installation prefix other than `/usr/local
' using `--prefix'
,
for
instance `--prefix=$HOME'. ===>这段话提示默认目录就为/usr/locar
……
[root@xpleaf ntp-
4.2
.4p7]# ./configure --prefix=/usr/local/ntp ===>指定安装目录
……
checking
for
gcc... gcc
……
config.status: creating Makefile
……
|
1
2
3
4
5
|
[root@xpleaf ntp-
4.2
.4p7]# make clean ===>make之前先clean
……
[root@xpleaf ntp-
4.2
.4p7]# make
……
[root@xpleaf ntp-
4.2
.4p7]# make check ===>执行make的检查
|
1
2
3
4
5
6
7
8
|
[root@xpleaf ntp-
4.2
.4p7]# make install
……
[root@xpleaf ntp-
4.2
.4p7]# cd /usr/local/ntp/
[root@xpleaf ntp]# ll
total
12
drwxr-xr-x
2
root root
4096
Aug
17
01
:
53
bin
drwxr-xr-x
2
root root
4096
Aug
17
01
:
53
lib
drwxr-xr-x
3
root root
4096
Aug
17
01
:
53
man
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
[root@xpleaf ~]# head main.c hello.c square.c cube.c makefile
==> main.c <==
#
include
<stdio.h>
int
main(
void
)
{
int
num;
hello();
scanf(
"%d"
,&num);
square(num);
cube(num);
return
0
;
}
==> hello.c <==
#
include
<stdio.h>
void
hello(
void
)
{
printf(
"Welcome to my procedure!\n"
);
printf(
"This procedure will get the number you input.\n"
);
printf(
"And then output the square and cube of the number.\n"
);
printf(
"Now,please input a number:"
);
}
==> square.c <==
#
include
<stdio.h>
void
square(
int
n)
{
printf(
"The square of %d is %d.\n"
,n,n*n);
}
==> cube.c <==
#
include
<stdio.h>
void
cube(
int
m)
{
printf(
"The cube of %d is %d.\n"
,m,m*m*m);
}
==> makefile <==
OBJS = main.o hello.o square.o cube.o
main: ${OBJS}
gcc -o $@ ${OBJS}
clean:
rm -f main ${OBJS}
|
1
2
|
[root@xpleaf ~]# cp hello.c hello.c.backup
[root@xpleaf ~]# cp makefile makefile.backup
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@xpleaf ~]# vim hello.c
#
include
<stdio.h>
void
hello(
void
)
{
printf(
"Version 2.0\n"
); ===>新增的内容
printf(
"Made by xpleaf.\n"
);
printf(
"Welcome to my procedure!\n"
);
printf(
"This procedure will get the number you input.\n"
);
printf(
"And then output the square and cube of the number.\n"
);
printf(
"Now,please input a number:"
);
}
[root@xpleaf ~]# vim makefile
OBJS = main.o hello.o square.o cube.o
main: ${OBJS}
gcc -o $@ ${OBJS}
clean:
rm -f main ${OBJS}
install: ===>新增install和unstall目标
cp -a main /usr/local/bin
unstall:
rm -f /usr/local/bin/main
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@xpleaf ~]# make clean main install
rm -f main main.o hello.o square.o cube.o
cc -c -o main.o main.c
cc -c -o hello.o hello.c
cc -c -o square.o square.c
cc -c -o cube.o cube.c
gcc -o main main.o hello.o square.o cube.o
cp -a main /usr/local/bin ===>最终将软件安装到/usr/local/bin
[root@xpleaf ~]# main ===>直接在命令行模式下执行
Version
2.0
Made by xpleaf.
Welcome to my procedure!
This procedure will
get
the number you input.
And then output the square and cube of the number.
Now,please input a number:
3
The square of
3
is
9
.
The cube of
3
is
27
.
[root@xpleaf ~]# make unstall ===>卸载安装的软件
rm -f /usr/local/bin/main
[root@xpleaf ~]# main
-bash: /usr/local/bin/main: No such file or directory
|
1
2
3
4
|
[root@xpleaf ~]# mv hello.c
new
/hello_new.c
[root@xpleaf ~]# mv makefile
new
/makefile_new
[root@xpleaf ~]# mv hello.c.backup hello.c
[root@xpleaf ~]# mv makefile.backup makefile
|
1
2
3
4
5
6
7
|
[root@xpleaf ~]# diff -Naur hello.c
new
/hello_new.c >> update_patch_file
[root@xpleaf ~]# diff -Naur makefile
new
/makefile_new >> update_patch_file
[root@xpleaf ~]# cat update_patch_file
--- hello.c
2015
-
08
-
17
07
:
58
:
13.289069939
+
0800
+++
new
/hello_new.c
2015
-
08
-
17
07
:
48
:
03.818069620
+
0800
@@ -
1
,
6
+
1
,
8
@@
……
|
1
2
3
|
[root@xpleaf ~]# patch -p0 < update_patch_file
patching file hello.c
patching file makefile
|
1
|
diff -Naur hello.c
new
/hello_new.c
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@xpleaf ~]# make clean main install
rm -f main main.o hello.o square.o cube.o
cc -c -o main.o main.c
cc -c -o hello.o hello.c
cc -c -o square.o square.c
cc -c -o cube.o cube.c
gcc -o main main.o hello.o square.o cube.o
cp -a main /usr/local/bin
[root@xpleaf ~]# main
Version
2.0
===>版本已经更新为Version
2.0
,与前面的测试结果一致
Made by xpleaf.
Welcome to my procedure!
This procedure will
get
the number you input.
And then output the square and cube of the number.
Now,please input a number:
3
The square of
3
is
9
.
The cube of
3
is
27
.
[root@xpleaf ~]# make unstall
rm -f /usr/local/bin/main
[root@xpleaf ~]# main
-bash: /usr/local/bin/main: No such file or directory ===>软件已经被卸载
===>达到与测试时同样的效果
|
静态函数库 | 动态函数库 | |
扩展名 | .a | .so |
编译行为 |
编译时直接整合到执行程序中,因此利用静态函数库编译的文件会比较大 |
动态函数内容没有整合到执行程序中,程序里只有一个“指向的位置”,在用到函数库时才会去读取函数库,编译的文件相对会比较小 |
独立执行的状态 |
可以独立执行,不需要向外部读取函数库内容 |
不能独立执行,函数库文件要存在且目录不能改变,函数库文件也不能随意移动或删除 |
升级难易程序 |
升级较难,需要将升级后的函数库重新整合到程序中 | 升级容易,只升级函数库即可,不需要对程序重新编译,因为程序与函数库只是一个指向的关系 |
1
2
3
4
|
[root@xpleaf ~]# vi /etc/ld.so.conf
include
ld.so.conf.d/*.conf
/usr/lib/gcc/
[root@xpleaf ~]# ldconfig ===>执行该命令后不会有任何信息输出
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@xpleaf ~]# ldd /usr/bin/passwd
linux-vdso.so.
1
=> (
0x00007fff85046000
)
libuser.so.
1
=> /usr/lib64/libuser.so.
1
(
0x00007f92b7d7f000
)
libcrypt.so.
1
=> /lib64/libcrypt.so.
1
(
0x00007f92b7b48000
)
libgobject-
2.0
.so.
0
=> /lib64/libgobject-
2.0
.so.
0
(
0x00007f92b78fb000
)
libgmodule-
2.0
.so.
0
=> /lib64/libgmodule-
2.0
.so.
0
(
0x00007f92b76f8000
)
libglib-
2.0
.so.
0
=> /lib64/libglib-
2.0
.so.
0
(
0x00007f92b73e1000
)
libpopt.so.
0
=> /lib64/libpopt.so.
0
(
0x00007f92b71d7000
)
libpam_misc.so.
0
=> /lib64/libpam_misc.so.
0
(
0x00007f92b6fd3000
)
libaudit.so.
1
=> /lib64/libaudit.so.
1
(
0x00007f92b6db7000
)
libselinux.so.
1
=> /lib64/libselinux.so.
1
(
0x00007f92b6b97000
)
libc.so.
6
=> /lib64/libc.so.
6
(
0x00007f92b6803000
)
libpam.so.
0
=> /lib64/libpam.so.
0
(
0x00007f92b65f5000
)
libfreebl3.so => /lib64/libfreebl3.so (
0x00007f92b637d000
)
libgthread-
2.0
.so.
0
=> /lib64/libgthread-
2.0
.so.
0
(
0x00007f92b6179000
)
libpthread.so.
0
=> /lib64/libpthread.so.
0
(
0x00007f92b5f5c000
)
librt.so.
1
=> /lib64/librt.so.
1
(
0x00007f92b5d53000
)
libdl.so.
2
=> /lib64/libdl.so.
2
(
0x00007f92b5b4f000
)
/lib64/ld-linux-x86-
64
.so.
2
(
0x000000317d400000
)
|
1
2
3
4
5
6
7
8
9
|
[root@xpleaf ~]# ldd -v /lib/libc.so.
6
/lib/ld-linux.so.
2
(
0x00829000
)
linux-gate.so.
1
=> (
0x004b1000
)
Version information: ===>使用-v参数可以增加其他版本信息的显示
/lib/libc.so.
6
:
ld-linux.so.
2
(GLIBC_PRIVATE) => /lib/ld-linux.so.
2
ld-linux.so.
2
(GLIBC_2.
3
) => /lib/ld-linux.so.
2
ld-linux.so.
2
(GLIBC_2.
1
) => /lib/ld-linux.so.
2
|