Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried your to compile your files into shared libs and then use them (I use cygwin).</p> <p>Here's for Foo:</p> <pre><code>cm@Gregor-PC ~/src $ gcc -I. -c --shared -o libFoo.so Foo.c </code></pre> <p>With the bin util nm you can check the symbols (grep for 'foo' to limit the output)</p> <pre><code>cm@Gregor-PC ~/src $ nm libFoo.so | grep foo 0000000a T _foo U _foobar </code></pre> <p>Where it gives an offset and says 'T' for terminated that tells you the symbol is defined in the lib.</p> <p>Now the FooBar lib has to linked against Foo in order to have the foo symbol</p> <pre><code>cm@Gregor-PC ~/src $ gcc -I. -L. --shared -o libFooBar.so FooBar.c libFoo.so cm@Gregor-PC ~/src $ nm libFooBar.so | grep foo 61f0111e T _foo 61f010e0 T _foobar </code></pre> <p>With this I can compile against FooBar only and get foo as a known symbol:</p> <pre><code>cm@Gregor-PC ~/src $ gcc -I. -o tst1 tst1.c libFooBar.so cm@Gregor-PC ~/src $ ./tst1 Function returned: 42 </code></pre> <p>So it seems to work OK.</p> <p>You can improve on your approach to modularize your C code by having header files that contain only public data types, exported function prototypes (declared as extern) and maybe even public global variables or constants. Such a header file declares the interface to the module and would have to be included where the module is used. </p> <p>This is explained with much more detail in the wonderful book <a href="http://rads.stackoverflow.com/amzn/click/0201419505" rel="nofollow">'Functional C'</a> (Hartel, Muller, 1997, Addison Wesley, <a href="http://eprints.eemcs.utwente.nl/1077/02/book.pdf" rel="nofollow">link</a> ) in the chapter on modules.</p> <p>The benefit is that your dependencies are clearer to see (as includes in the source files) and you don't have to have that unsightly extern declaration in the Foo source. </p> <p>For your example:</p> <pre><code>/* Foo.h */ extern int foo(int id); /* exported to FooBar */ /* FooBar.h */ extern int foobar(); /* export to Foo */ extern void do_something_else(); /* export to tst1 */ /* Foo.c */ #include &lt;Foo.h&gt; #include &lt;FooBar.h&gt; int do_something() { return 11; } int foo(int id){ int var; switch (id){ case 1: var = do_something(); break; case 2: /* the module that gets here, has the required functions defined in it */ var = foobar(); } return var; } /* FooBar.c */ #include &lt;stdio.h&gt; #include &lt;Foo.h&gt; #include &lt;FooBar.h&gt; int foobar(){ return 42; } void do_something_else(){ int ret = foo(2); printf("Function returned: %d", ret); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload