Results 1 to 6 of 6
when I compile against a library like this
Code:
gcc -o main main.c libmy.a
and this
Code:
gcc -o main main.c ./libmy.a
What is the difference ?
Is it that ...
- 03-31-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Compiling option
when I compile against a library like this
and thisCode:gcc -o main main.c libmy.a
What is the difference ?Code:gcc -o main main.c ./libmy.a
Is it that the latter case may search in standard dir for-C-lib first ;
and the second will immediately take the library in the current ('cd'ed) working directory ?
And where will the compiler search - in the first case ?
- 03-31-2008 #2Yes.when I compile against a library like this
and thisCode:gcc -o main main.c libmy.a
What is the difference ?Code:gcc -o main main.c ./libmy.a
Is it that the former case may search in standard dir for-C-lib first ;
and the second will immediately take the library in the current ('cd'ed) working directory ?
To find the answer to that, do this at the command line:And where will the compiler search - in the first case ?
You can modify this by setting the environment variable LIBRARY_PATH. Consult additional details here.Code:gcc -print-search-dirs
You can also add to this list by using the command line option:
orCode:-Lmylibrarydirectory
Consult additional details here.Code:-L/home/myname/mylibrarydirectory
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 04-01-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
Yes..But I have another question :
When I use static library , it works with both methods(mylib.a and ./mylib.a)
but with dynamic library , it works only with ./mylib.so , the other case(mylib.so) issues an error message : "cannot open shared object file mylib.so.." when running the program ( compiled well , but when run , above error ..)
So why is it like that ?
And about the search path , I only have to put the library file in some directory and make the option to search there ? What about the .h file ?
- 04-01-2008 #4The library is not in your shared library search path, as you surmise when you ask the following question:with dynamic library , it works only with ./mylib.so , the other case(mylib.so) issues an error message : "cannot open shared object file mylib.so.." when running the program ( compiled well , but when run , above error ..)
So why is it like that ?
Not an option exactly, but you're close. Rather than have me paraphrase the answer for you, it would be better if you read some actual documentation about shared libraries.I only have to put the library file in some directory and make the option to search there ?
Depends on whether you sayWhat about the .h file ?
orCode:#include <fred.h>
Details are found here.Code:#include "fred.h"
--
Bill
Old age and treachery will overcome youth and skill.
- 04-02-2008 #5Just Joined!
- Join Date
- Mar 2008
- Posts
- 12
But why it happens with dynamic library only ? (My previous question)
Yes..But I have another question :
When I use static library , it works with both methods(mylib.a and ./mylib.a)
- 04-02-2008 #6I can answer that on two levels. The first-level answer is fairly useless: it happens with dynamic libraries only, because they wrote it that way.But why it happens with dynamic library only ?
So why did they write it that way? The second-level answer is that dynamic libraries are often loaded by various users, with various programs, so that it makes sense to look for them in common locations only (unless you override that by specifying a particular directory in the gcc command). If more than one program sharing the same dynamic library is running at the same time, only one copy of the library is in main memory, and the programs share that copy.
This is why dynamic libraries are alternately called shared libraries. (The "so" in the filename's extension means "shared object").--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote