osx - Beginner's question, trying to understand how the linker searches for a static library -
I have a work setup, where all the files are in the same directory (desktop) is the terminal output such as:
$ gcc -c mymath.c $ ar r mymath.a mymath.o ar: create archive mymath.a $ ranlib mymath.a $ gcc test.c Mymath.a -o trial $ ./test Hello World! 3.14 1.77 10.20
Files:
mymath.c:
float mysqrt (float n) {return 10.2; }
test.c:
#include & lt; Math.h> # Include & lt; Stdio.h & gt; #include "mymath.h" Main () {printf ("Hello World! \ N"); Float x = sqrt (M_PI); Printf ("% 3.2f \ n", M_PI); Printf ("% 3.2f \ n", sqrt (M_PI)); Printf ("% 3.2f \ n", mysqrt (M_PI)); Return 0; }
Now, I move the archive mymath.a to a subdirectory / temp. I am not able to get the link to work:
$ gcc test.c mymath.a -o test -l / users / telliott_admin / desktop / temp / mymath.a i686- Apple-darwin10-gcc-4.2.1: mymath.a: Library for no file or directory $ gcc test.c -o test -i / users / telliott_admin / desktop / temp -lmymath ld: -lmymath collect2 Could not find: Ld 1 exit status returned
What am I missing? Which resource would you recommend?
Update: Thanks for your help, all answers were basically correct. I have blogged about it.
To include math libraries, use -lm, not -lame, besides you To add a link to the link, use the sub-directory along with the sub-directory (-I just include the header for the compilation).
You can compile and link:
GCC test.c -o test -i / users / telliott_admin / desktop / temp / user / teleteot_admin / desktop / temp / Mymath.a
or
gcc test.c -o test -i / users / telliott_admin / desktop / temp -l / user / telliott_admin / Desktop / temp -lmymath
where mymath.a has been renamed Libmymath.a.
For comments (search for "bad programming") -l:
Comments
Post a Comment