您的位置:首页 > 运维架构 > Linux

我是这样学习Linux下C语言编程的-编译命令gcc的使用

2011-09-09 06:35 671 查看
gcc这个大名鼎鼎的编译器软件,如果你用UNIX类系统,当然包括Linux,一定要知道。
gcc - GNU project C and C++ compiler

1、基本用法

一般用作C语言编译器时是gcc,而用作C++语言编译器时是g++。
其语法结构为:
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] infile...

虽然看起来选项非常多,好象挺高深的。但只有一个infile是必需选项,故我们初学者只需要知道这样的形式就行了:
gcc source-files
即用任何一种文本编译器编写C语言源程序,保存为一个UNIX下格式的文本文件,比如文件名为test.c,那么用下面这个命令把源程序变成可执行程序就行了:
gcc test.c
如果你是用Windows下的文件编辑软件写的源程序,在用gcc编译之前最好用命令dos2unix转换一下文件格式。因为早期的gcc版本不能识别windows下的文件格式。会报奇怪的错误。
dos2unix命令由软件包tofrodos提供,软件包信息如下:Description: Converts DOS <-> Unix text files, alias tofromdos
DOS text files traditionally have CR/LF (carriage return/line feed) pairs
as their new line delimiters while Unix text files traditionally have
LFs (line feeds) to terminate each line.
.
Tofrodos comprises one program, "fromdos" alias "todos", which converts
text files to and from these formats. Use "fromdos" to convert DOS
text files to the Unix format, and "todos" to convert Unix text files
to the DOS format.
.
This functionality is also available via the dos2unix/unix2dos symlinks.
.
Homepage: http://www.thefreecountry.com/tofrodos/index.shtml
即实现Windows文本文件格式和Linux文本文件格式的转换。
因为Windows下文本文件以CR和LF行结束符,而Linux下文本文件以LF作为行结束符。所以在Linux下编写的文本文件到Windows下用记事本等软件打开时会出现所有内容挤在一行的现象。
用C语言的习惯来说就是:Windows下文本文件行结束符是'\r'和'\n'而Linux下是'\n',即Windows下是十六进制的0x0d和0x0a,而Linux下是0x0a
用上面的编译命令:
gcc test.c
将在当前目录下产生一个可执行程序a.out,要运行程序试试?输入命令:
./a.out
如果想要产生一个自己命令的程序,即程序名不是默认的a.out,可以用这种命令格式:
gcc -o out-file source-file
比如:
gcc -o myprog test.c
将到源程序test.c变成一个可执行程序myprog,运行这个命令看看你的成果吧:
./myprog

2、编译多个文件
如果你的程序源代码比较多,并且分在几个源文件里面,互相之前有调用现象,就要用gcc编译多个源文件了。命令格式如下:
gcc -o out-file source-file-a source-file-b source-file-c ...
可以把所有源代码文件联合编译生成一个程序out-file

3、代码风格
很多人都会讲代码风格问题,但这应该是机器做的事而不是人做的事!
Linux下有一个很好的软件indent可以用来把代码按照一定的风格进行整理。该软件包信息如下:Description: C language source code formatting program
The `indent' program changes the appearance of a C program by
inserting or deleting whitespace.
.
`indent' also provides options for controlling the alignment of braces and
declarations, program indenting, and other stylistic parameters, including
formatting of both C and C++ comments.

比如我们编写了一个源程序test.c,代码格式比较乱,如下:#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int port = 0;
printf("program begin\n");
if (argc != 3) {
printf("程序参数个数不对!正确输入方法如下:\n%s ip-address port\n", argv[0]);
exit(0);
}
if(atoi(argv[2]) <0 || atoi(argv[2]) > 65535) {printf("端口输入不对!应该是0-65535的值。程序取默认值3456\n"); port = 3456;}
else port = atoi(argv[2]);
printf("命令如下:%s %s %d\n", argv[0], argv[1], port);
return 0;
}

用indent命令格式化一下,命令如下:
indent test.c
源代码变成下面的样子了:#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main (int argc, char **argv)
{
int port = 0;
printf ("program begin\n");
if (argc != 3)
{
printf
("程序参数个数不对!正确输入方法如下:\n%s ip-address port\n",
argv[0]);
exit (0);
}
if (atoi (argv[2]) < 0 || atoi (argv[2]) > 65535)
{
printf
("端口输入不对!应该是0-65535的值。程序取默认值3456\n");
port = 3456;
}
else
port = atoi (argv[2]);
printf ("命令如下:%s %s %d\n", argv[0], argv[1], port);
return 0;
}

这种代码风格是GNU的风格,是indent的默认格式。
但我们多数写程序的人可能都是学的Kernighan & Ritchie代码风格,用如下命令格式化:
indent -kr test.c
代码格式化成这样了:#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int port = 0;
printf("program begin\n");
if (argc != 3) {
printf
("程序参数个数不对!正确输入方法如下:\n%s ip-address port\n",
argv[0]);
exit(0);
}
if (atoi(argv[2]) < 0 || atoi(argv[2]) > 65535) {
printf
("端口输入不对!应该是0-65535的值。程序取默认值3456\n");
port = 3456;
} else
port = atoi(argv[2]);
printf("命令如下:%s %s %d\n", argv[0], argv[1], port);
return 0;
}

记住:本该机器来做的事,不要手工去做!
当然,如果你用indent格式化时报错或格式化后发现不对劲,一定是你的代码里有问题,比如少了一个“}“

4、养成好的习惯
大家都写C程序,可有人的代码总会有问题,而写得好一点的就会基本没错误。
这需要一个慢慢练习的过程,但作为初学者,你在编译程序时就加上gcc的-Wall选项吧。即命令格式为:
gcc -Wall -o out-file source-file
这样,程序中每行代码有问题都有提到提示。初学者把每个Warning和Error都弄清楚对养成好的编程习惯是相当有用的。

5、为调试程序作准备
可能我们的程序编译通过了,也生成了可执行程序,但可能运行的时候出错,甚至刚开始运行一段时间都没问题后来某一天出了问题。
有些问题一看错误提示就明白了。但有些可能花较长时间都想不明白,只能调试程序了。
为了保证可以调试程序,我们在用gcc编译程序时要加上-g选项,即命令格式如下:
gcc -Wall -g -o out-file source-file
这个命令就会把源代码编译产生的可执行程序里加上调试信息,如果程序出错出现Core dump文件时可以从Core dump文件里快速定位到程序哪行出了问题。

转自:http://zhoulifa.bokee.com/6099379.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: