C:链接驱动程序程序中的其他C文件



好吧,我正在制作一个可以实现每种形式的数据结构的程序堆栈。并为每个文件创建单独的文件,现在我想在单个驱动程序程序中使用每个单独的文件。我已经将文件链接为:

#include"filename.c"

但是出现错误 no这样的文件或目录。是的。

您不包括.c文件,而不是.h文件。

假设您的文件夹包含您的main.c文件和datastructs.c文件,请创建一个包含所有函数声明的datastructs.h文件。

datastructs.c

#include <stdio.h>
#include "datastructs.h"
void hello() {
    printf("hello, world!n");
}

datastructs.h

void hello();

现在,在main.c中 - 包含main函数的C文件 - 包括datastructs.h文件并调用您想要的所有功能:

#include "datastructs.h"
void main() {
    hello();
}

确保编译您使用的每个来源,它将确保正确链接所有内容:

gcc datastructs.c main.c -o main

这是一种非常基本的方法,那里还有更多 - 甚至可能比这更好 - 但它可以完成工作。

确保查看Makefilemake的工作原理,这样您就可以更好地处理此类任务。

最新更新