代码目录结构 – 库设计

下面是代码结构,其中stackQueuetree文件夹代码依赖于list文件夹代码,

 ../Computing >ls HashTable list Queue recursion stack tree 

list tree/rootedTree文件夹中复制的list文件夹,不像推荐的方法包含依赖的头文件, 这里提到的,

 ../Computing/tree/rootedTree >ls lcrsImpl.c list main.c multiWalkImpl.c tree.h 

这是rootedTree文件夹的不完整代码。

为避免代码重复List文件夹,如何维护代码结构?

你似乎有以下结构:

 whatever/ list/ ... list.h ... queue/ ... <- some files have an #include "list/list.h"; list/ <- duplicate tree/ rootedTree/ ... list/ <- duplicate tree.h <- has an #include "list/list.h"; test.c <- #include "tree.h" 

在此代码中,首先转到要编译内容的文件夹,然后在文件夹中编译它们:

  you@somewhere:~/whatever/tree/rootedTree$ gcc -Wall -I. -g *.c -o test 

#include语句的路径与调用编译器的位置有关(只要包含-I.参数;谢谢,@ jean-françois-fabre) 。 所以你很可能有以下(非重复)结构:

 whatever/ list/ ... list.h ... queue/ ... <- some files have an #include "list/list.h"; no list/ duplicate tree/ rootedTree/ ... tree.h <- has an #include "list/list.h"; no list/ duplicate testTree.c <- #includes "tree/rootedTree/tree.h" and others 

而且, 任何文件夹中 ,你都可以写

 you@somewhere:~/whatever$ gcc -Wall -I. -g */*.c testTree.c -o testTree 

并获得一个可执行文件来测试。 由于直接调用编译器很无聊,特别是如果你只想编译你实际进行了更改的代码部分,你通常会使用某种项目定义文件( Makefile , Scons ,...)为你处理。

复制很糟糕,并且(几乎)始终有一种方法可以避免它。