Code directory structure - Library design

与世无争的帅哥 提交于 2019-12-25 08:17:03

问题


below is the code structure, where stack, Queue, tree folder code rely on list folder code,

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

list folder duplicated in tree/rootedTree folder unlike recommended approach to include dependent header files, mentioned here,

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

here is the incomplete code for rootedTree folder.

To avoid code duplication of List folder, How to maintain the code structure?


回答1:


You seem to have the following structure:

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"

In this code, you first go to the folder where you want to compile things, and then compile them inside the folder:

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

The paths of the #include statements are relative to where the compiler is invoked from (as long as you include the -I. argument; thanks, @jean-françois-fabre). So you can very well have the following (non-duplicated) structure:

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

and, from within the whatever folder, you can write

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

and get an executable to test. Since calling the compiler directly is boring, especially if you only want to compile the parts of the code where you have actually made changes, you generally use some sort of project definition file (Makefile, Scons, ...) to handle that for you.

Duplication is bad, and there is (almost) always a way to avoid it.



来源:https://stackoverflow.com/questions/41281332/code-directory-structure-library-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!