gcc -o :指定输出的可执行文件的名称,不指定时为a.out
gcc -c:只编译不链接,生成.o文件。这时出现未定义的函数,即在其他文件里的函数并不会报错。如果是结构体定义在其他文件里,则会发生报错。
gcc -g,是为了gdb用。
如下
//test1.c
#include <stdio.h>
void func_a(){
printf("FUNC_A\n");
}
//test2.c
#include <stdio.h>
int main(void)
{
func_a();
//func_b();
printf("hello\n");
return 0;
}
gcc -c test2.c test1.c
gcc -g -o test test2.c test1.o
生成test文件
结果
头文件 #ifndef …#define…#endif
防止头文件多次编译。
结构体一般定义在.c文件内部或.h文件中。
定义在.c文件内主要是单个文件内部用。
定义在.h文件中则是为了在多个.c文件中使用。
结构体定义在.c文件中时,可以用该文件的函数返回值为结构体地址的方式初始化外部.c文件的结构体指针。
如:
//test1.c
#include <stdio.h>
#include"test2.h"
int main(void)
{
func_a();
struct St *s= func_b();
printf("hello\n");
// printf("%s\n",s.name);
return 0;
}
//test2.h
#ifndef TEST2_H
#define TEST2_H
struct St;
void func_a();
struct St * func_b();
#endif
test2.c
#include <stdio.h>
#include "test2.h"
struct St{
int id;
char *name;
};
void func_a(){
printf("FUNC_A\n");
}
struct St *func_b(){
struct St s = {1,"Luck"};
return &s;
}
//gcc -g -o test test1.c test2.c编译
若在test1.c中采用如下方式初始化结构体则会报错
#include <stdio.h>
#include"test2.h"
int main(void)
{
func_a();
struct St *s= = {1,"Luck"};
printf("hello\n");
// printf("%s\n",s.name);
return 0;
}