Compiling a Simple C Program
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
Example1
- 建立文件vim hello.c,键入C源码
- 编译文件gcc -Wall hello.c -o hello(指定编译文件名为hello,默认为a.out)
- 执行文件./hello
Compiling Multiple Source Files
- A program can be split up into multiple(许多个) files.This makes it easier to edit and understand,especially in the case of large programs.
- The difference between the two froms of the include statement #include "FILE.h" and #include <FILE.h> is:
-- #include "FILE.h" searches for "FILE.h" in the current directory before looking in the system header file directories.
-- #include <FILE.h> searches the system header files,but does not look in the current directory by default.
Example2
- 建立文件vim hello.h
void hello(const char* string);
- 建立文件vim hello.c
#include <stdio.h>
#include "hello.h"
void hello(const char* string)
{
printf(string);
}
- 建立文件vim main.c
#include <stdio.h>
#include "hello.h"
int main(void)
{
hello("hello world!");
return 0;
}
- 编译文件gcc -Wall hello.c main.c -o newhello
- 执行文件./newhello
2020.10.8