Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12

    Compiling option

    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 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 ?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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 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 ?
    Yes.
    And where will the compiler search - in the first case ?
    To find the answer to that, do this at the command line:
    Code:
    gcc -print-search-dirs
    You can modify this by setting the environment variable LIBRARY_PATH. Consult additional details here.

    You can also add to this list by using the command line option:
    Code:
    -Lmylibrarydirectory
    or
    Code:
    -L/home/myname/mylibrarydirectory
    Consult additional details here.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just 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 ?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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 ?
    The library is not in your shared library search path, as you surmise when you ask the following question:
    I only have to put the library file in some directory and make the option to search there ?
    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.
    What about the .h file ?
    Depends on whether you say
    Code:
    #include <fred.h>
    or
    Code:
    #include "fred.h"
    Details are found here.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just 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)

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    But why it happens with dynamic library only ?
    I 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.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...