gcc编译

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值