[Linux]编译一个C程序:./configure 、 mak

2018-09-12  本文已影响76人  AkuRinbu

举例说明

查看是否安装了编译器和make

anno@anno-m:~$ which gcc
/usr/bin/gcc
anno@anno-m:~$ which clang
/usr/bin/clang
anno@anno-m:~$ which make
/usr/bin/make

获取源代码

1、首先,创建一个src目录用于存放源代码;

2、然后使用ftp下载源代码至该目录;

3、完成后,本地的~/src目录就会有一个压缩的tar文件形式存在的源代码包;

anno@anno-m:~$ mkdir src
anno@anno-m:~$ ls
Desktop    Downloads         Music     Public  Templates
Documents  examples.desktop  Pictures  src     Videos

anno@anno-m:~$ cd src
anno@anno-m:~/src$ pwd
/home/anno/src
anno@anno-m:~/src$ ftp -p ftp.gnu.org
Name (ftp.gnu.org:anno): anonymous
ftp> cd gnu/diction

ftp> ls
-rw-r--r--    1 3003     65534       68940 Aug 28  1998 diction-0.7.tar.gz
-rw-r--r--    1 3003     65534       90957 Mar 04  2002 diction-1.02.tar.gz
-rw-r--r--    1 3003     65534      141062 Sep 17  2007 diction-1.11.tar.gz
-rw-r--r--    1 3003     65534         189 Sep 17  2007 diction-1.11.tar.gz.sig
ftp> get  diction-1.11.tar.gz
ftp> bye

anno@anno-m:~/src$ ls
diction-1.11.tar.gz

4、解压缩:tar tzvf tarfile | head(检查tar文件的内容)

anno@anno-m:~/src$ tar tzvf diction-1.11.tar.gz | head
-rw-r--r-- michael/user  35068 2007-07-31 04:47 diction-1.11/COPYING
-rw-r--r-- michael/user   9416 2007-08-03 15:03 diction-1.11/INSTALL
-rw-r--r-- michael/user   3920 2007-08-03 18:05 diction-1.11/Makefile.in
-rw-r--r-- michael/user   1448 2007-08-30 18:20 diction-1.11/README
-rw-r--r-- michael/user    152 2007-08-30 16:08 diction-1.11/NEWS
-rwxr-xr-x michael/user 144080 2007-08-30 16:06 diction-1.11/configure
-rwxr-xr-x michael/user  13184 2007-08-03 15:03 diction-1.11/install-sh
-rw-r--r-- michael/user   2621 2007-03-31 05:45 diction-1.11/de
-rw-r--r-- michael/user  24830 2007-03-31 05:45 diction-1.11/en
-rw-r--r-- michael/user  25043 2007-03-31 05:45 diction-1.11/en_GB
anno@anno-m:~/src$ tar xzf diction-1.11.tar.gz 
anno@anno-m:~/src$ ls
diction-1.11  diction-1.11.tar.gz
anno@anno-m:~/src$ 

检查源代码树

1、进入生成的目录 cd diction-1.11

anno@anno-m:~/src$ cd diction-1.11

anno@anno-m:~/src/diction-1.11$ ls
config.guess  de            diction.spec.in  getopt.c      misc.c  sentence.c
config.h.in   de.po         diction.texi.in  getopt.h      misc.h  sentence.h
config.sub    diction.1.in  en               getopt_int.h  NEWS    style.1.in
configure     diction.c     en_GB            INSTALL       nl      style.c
configure.in  diction.pot   en_GB.po         install-sh    nl.po   test
COPYING       diction.spec  getopt1.c        Makefile.in   README
anno@anno-m:~/src/diction-1.11$ ls *.c
diction.c  getopt1.c  getopt.c  misc.c  sentence.c  style.c
anno@anno-m:~/src/diction-1.11$ ls *.h
getopt.h  getopt_int.h  misc.h  sentence.h
anno@anno-m:~/src/diction-1.11$ less diction.c

生成程序 ./configure

1、运行 ./configure

anno@anno-m:~/src/diction-1.11$ ./configure

configure: creating ./config.status
config.status: creating Makefile
config.status: creating diction.1
config.status: creating diction.texi
config.status: creating diction.spec
config.status: creating style.1
config.status: creating test/rundiction
config.status: creating config.h

anno@anno-m:~/src/diction-1.11$ less Makefile
diction:    diction.o sentence.o misc.o getopt.o getopt1.o
        $(CC) -o $@ $(LDFLAGS) diction.o sentence.o misc.o \
        getopt.o getopt1.o $(LIBS)


diction.o:  diction.c config.h getopt.h misc.h sentence.h
getopt.o:   getopt.c getopt.h getopt_int.h
getopt1.o:  getopt1.c getopt.h getopt_int.h
misc.o: misc.c config.h misc.h
sentence.o: sentence.c config.h misc.h sentence.h
style.o:    style.c config.h getopt.h misc.h sentence.h
.c.o:
        $(CC) -c $(CPPFLAGS) $(CFLAGS) $<
CC=     gcc

2、运行make,生成diction程序

anno@anno-m:~/src/diction-1.11$ make
运行 make 之前 运行 make 之后

安装程序

1、使用sudo make install 运行安装

anno@anno-m:~/src/diction-1.11$ sudo make install

2、安装结束后查看程序是否运行

anno@anno-m:~/src/diction-1.11$ which diction
/usr/local/bin/diction

anno@anno-m:~/src/diction-1.11$ man diction

anno@anno-m:~$ ls /usr/local/bin
diction  style

参考资料

ftp> ls
200 PORT command successful. Consider using PASV.
425 Failed to establish connection.
https://superuser.com/questions/356138/cant-connect-to-ftp-server-425-unable-to-build-data-connection-connection-tim/356142#356142
连接ftp后,使用ls命令报错了,
书上原本使用的是ftp ftp.gnu.org
改成 ftp -p ftp.gnu.org,加个-p 选项

《Linux命令行大全》
第23章 编译程序
https://www.jianshu.com/p/accaef5bc096

上一篇下一篇

猜你喜欢

热点阅读