[An Introduction to GCC 学习笔记] 03 hello world、编译多个文件

helloworld

The classic example program for the C language is:

#include <stdio.h>

int main(void)
{
	printf("Hello world!\n");
	return 0;
}

To compile the file with gcc, use the following command:

$ gcc -Wall hello.c -o hello

在众多的警告选项之中,最常用的就是-Wall选项。该选项能发现程序中一系列的常见错误警告。
该选项相当于同时使用了下列所有的选项:
◆unused-function:遇到仅声明过但尚未定义的静态函数时发出警告。
◆unused-label:遇到声明过但不使用的标号的警告。
◆unused-parameter:从未用过的函数参数的警告。
◆unused-variable:在本地声明但从未用过的变量的警告。
◆unused-value:仅计算但从未用过的值得警告。
◆Format:检查对printf和scanf等函数的调用,确认各个参数类型和格式串中的一致。
◆implicit-int:警告没有规定类型的声明。
◆implicit-function-:在函数在未经声明就使用时给予警告。
◆char-subscripts:警告把char类型作为数组下标。这是常见错误,程序员经常忘记在某些机器上char有符号。
◆missing-braces:聚合初始化两边缺少大括号。
◆Parentheses:在某些情况下如果忽略了括号,编译器就发出警告。
◆return-type:如果函数定义了返回类型,而默认类型是int型,编译器就发出警告。同时警告那些不带返回值的 return语句,如果他们所属的函数并非void类型。
◆sequence-point:出现可疑的代码元素时,发出报警。
◆Switch:如果某条switch语句的参数属于枚举类型,但是没有对应的case语句使用枚举元素,编译器就发出警告(在switch语句中使用default分支能够防止这个警告)。超出枚举范围的case语句同样会导致这个警告。
◆strict-aliasing:对变量别名进行最严格的检查。
◆unknown-pragmas:使用了不允许的#pragma。
◆Uninitialized:在初始化之前就使用自动变量。
查看gcc的版本:

gcc --version

在这里插入图片描述
gcc编译hello world与执行:
在这里插入图片描述

编译一个有问题的程序

#include <stdio.h>

int main(void)
{
        printf("Two plus two is %f\n", 4);
        return 0;
}

编译后有warning,但是没有error:
在这里插入图片描述
运行后,不符合预期:
在这里插入图片描述
在一些gcc版本中,如果不加-Wall编译时,并不会报Warning,这也体现了-Wall编译,给用户提示Warning信息的重要性。

Compiling Multiple Source Files

  • A program can be split up into multiples files. This makes it easier to edit and understand, especially in the case of large programs.
  • The difference between the tow forms of the include statement
    #include “FILE.h” and #include <FILE.h> is
    • 1 include “FILE.h” searches for “FILE.h” in the current directory before looking in the system header file directories.
    • 2 include <FILE.h>searches the system header files, but does not look in the current directory by default.

编译多个文件的命令

gcc -Wall -file1.c -file2.c -o file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值