004_wz_bbk_用GCC编译第一个C程序及多文件编程

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

  1. 建立文件vim hello.c,键入C源码
  2. 编译文件gcc -Wall hello.c -o hello(指定编译文件名为hello,默认为a.out)
  3. 执行文件./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

  1. 建立文件vim hello.h
void hello(const char* string);
  1. 建立文件vim hello.c
#include <stdio.h>
#include "hello.h"

void hello(const char* string)
{
printf(string);
}
  1. 建立文件vim main.c
#include <stdio.h>
#include "hello.h"

int main(void)
{
hello("hello world!");
return 0;
}
  1. 编译文件gcc -Wall hello.c main.c -o newhello
  2. 执行文件./newhello
exp2

2020.10.8

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容