编译多个源文件

标签 c

我有四个包含 C 代码的文件。

Headers.h - (包含所有必需的) header

AddStudent.h - 该文件包含 Headers.h 还引入了一些函数委托(delegate)

AddStudent.c - 包含 AddStudent.h

中描述的函数

main.c - 包含 main()

问题是如何用cc编译代码?

最佳答案

就您而言,您可能只需要:

cc main.c AddStudent.c

正确的做法是创建一个 makefile。这是一个(可能有点幼稚)示例:

myapp: main.o AddStudent.o
  cc -o myapp main.o AddStudent.o

main.o: main.c AddStudent.h Headers.h
  cc -c -o main.o main.c

AddStudent.o: AddStudent.c AddStudent.h Headers.h
  cc -c -o AddStudent.o AddStudent.c

了解 make 的最佳位置是 GNU Make Manual .

附注 - 如果您开始学习 C,您可能需要查看 clang 。除了支持没有特殊标志的 C99 以及编译速度更快之外,它还提供了比 gcc 更好的错误消息。

关于编译多个源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762855/

相关文章:

C 内存集警告

c - 为什么不能在 ubuntu 上链接 64 位静态 libgcc

c - C中int数组的二维数组?

C语言: Search text file for a "word" and store the value into int and flout type variables

c - 为什么 inet_aton 返回的十六进制数是倒序的?

c# - 在 C# 应用程序中使用 C-DLL?

c - char* 的可变二维数组作为 C 中的返回值

c - 多线程 C 应用程序中的访问冲突

c - 本地范围内的静态

python数组地址传递给C