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)
- 執(zhí)行文件./hello
exp1
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
- 執(zhí)行文件./newhello
exp2
2020.10.8