Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does the computer know whether to use static or dynamic linking in this case
    text
    copied!<p>I'm getting thru Head First C, doing all the programming examples. Chap 8 starts with static linking. We use these four commands (Cygwin):</p> <pre><code>gcc -I./includes -c hfcal.c -o hfcal.o gcc -I./includes -c elliptical.c -o elliptical.o ar -rcs ./libs/libhfcal.a hfcal.o gcc elliptical.o -L./libs -lhfcal -o elliptical </code></pre> <p>Assuming I typed that correctly, it prints out a few lines showing calories burned on an elliptical machine.</p> <p>Next they introduce dynamic linking. It's not the clearest chapter in the book and I'm still a little fuzzy on it. Compounding that is the fact that I'm not sure whether the new code has typos or not. The text says the code is the same as before but leaves out the dot before the slash and even leaves off the s at the end of includes in one case. I put the dot back in and only changed the parts they said should be changed. (I went to their errata page and saw nothing relevant.) Here's the new code:</p> <pre><code>gcc -I./includes -fPIC -c hfcal.c -o hfcal.o //Got a warning, fPIC ignored, as book predicted gcc -shared hfcal.o -o ./libs/libhfcal.dll gcc -I./includes -c elliptical.c -o elliptical.o gcc elliptical.o -L./libs -lhfcal -o elliptical </code></pre> <p>Notice that except for the the ignored <code>-fPIC</code> and the second line, these are the same commands as the first case, in slightly different order. </p> <p>After I successfully ran ./elliptical, I wondered if I was supposed to in fact use a different directory and that maybe the missing dot was intentional. I never rm'd the archive .a file and it was in the same directory as the new dll file. How would the compiler know whether to use libhfcal.a or libhfcal.dll, since they are both there and I call them the same way? Is it because of the order?</p> <p>Any good resources where I can read a clearer explanation, esp with code I can try?</p>
 

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