C语言编译多个C文件
作者:三味书屋 来源: 2022/1/7 14:31:59

1、建立6个文件,分别为pa.c、pa.h、pb.c、pb.h、pc.c、pc.h、test.c。基本思路是在test.c文件里面引用pa.c、pb.c、pc.c文件里的内容。

pa.c

// pa.c
# include <stdio.h>
# include "pa.h"

void A_print()
{
	printf("A\n");
}


pa.h

// pa.h
# ifndef PA_H
# define PA_H
void A_print();
# endif


pb.c

// pb.c
# include <stdio.h>
# include "pb.h"

void B_print()
{
	printf("B\n");
}


pb.h

// pb.h
# ifndef PB_H
# define PB_H
void B_print();
# endif


pc.c

// pc.c
# include <stdio.h>
# include "pc.h"

void C_print()
{
	printf("C\n");
}


pc.h

// pc.h
# ifndef PC_H
# define PC_H
void C_print();
# endif


test.c

// test.c
# include "pa.h"
# include "pb.h"
# include "pc.h"
int main (void)
{
	A_print();
	B_print();
	C_print();
	return 0;
}
编译命令:


-o:指定输出文件名
-E:只进行预处理,不编译
-S:只编译,不汇编
-c:只编译、汇编,不链接
-g:包含调试信息
-I:指定include包含的搜索目录
-o:输出成指定文件名
预编译处理,生成.i文件:gcc -E pa.c -o pa.i
编译处理,生成.s文件:gcc -S pa.i -o pa.s
汇编处理,生成.o文件:gcc -c pa.i -o pa.o


gcc -E pb.c -o pb.i
gcc -S pb.i -o pb.s
gcc -c pb.i -o pb.o

gcc -E pc.c -o pc.i
gcc -S pc.i -o pc.s
gcc -c pc.i -o pc.o

gcc -E test.c -o test.i
gcc -S test.i -o test.s
gcc -c test.i -o test.o

链接处理,生成test.exe文件
gcc pa.o pb.o pc.o test.o -o test


称      呼:
联系方式:
您的评论:
技术支持:l.w.dong@qq.com www.luweidong.cn
广州市   wx:lwdred
Copyright © 2014 三味书屋 All Rights Reserved
技术支持:l.w.dong@qq.com  sitemap xml  sitemap html

粤公网安备44010602011869号

粤ICP备13031080号-1