below is the code structure, where
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 <a href="https://stackoverflow.com/a/597329/3317808" rel="nofollow">here</a>,
../Computing/tree/rootedTree >ls lcrsImpl.c list main.c multiWalkImpl.c tree.h
<a href="https://github.com/shamhub/Computing/tree/master/7_Tree/Rooted_tree/Rooted_tree_Implementation_c" rel="nofollow">here</a> is the incomplete code for
To avoid code duplication of List folder, How to maintain the code structure?Answer1:
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 <em>(as long as you include the
-I. argument; thanks, @jean-françois-fabre)</em>. 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, <strong>from within the <em>whatever</em> folder</strong>, 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 (<a href="https://www.gnu.org/software/make/manual/make.html" rel="nofollow">Makefile</a>, <a href="http://scons.org/" rel="nofollow">Scons</a>, ...) to handle that for you.
Duplication is bad, and there is (almost) always a way to avoid it.