Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I have a shared library A which uses shared library B, and an application P which relies on A. I don't want to link P against A and ...
  1. #1
    qcp
    qcp is offline
    Just Joined!
    Join Date
    Jul 2008
    Posts
    1

    gcc link shared library against another shared library

    Hi all,

    I have a shared library A which uses shared library B, and an application P which relies on A.

    I don't want to link P against A and B, but to link P against A only, and A against B.

    Thanks for your answers

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This will only make a difference if two functions with the same name exist in both libraries, right?

    Here ya go.
    Code:
    #!/bin/bash
    
    cat > a.h <<EOD
    void fred(void);
    void barney(void);
    EOD
    
    cat > b.h <<EOD
    void wilma(void);
    void barney(void);
    EOD
    
    cat > a1.c <<EOD
    #include <stdio.h>
    #include "b.h"
    void fred(void)
    {
      printf("this is fred\n");
      wilma();
    }
    EOD
    
    cat > a2.c <<EOD
    #include <stdio.h>
    #include "b.h"
    void barney(void)
    {
      printf("this is barney from library A\n");
    }
    EOD
    
    cat > b1.c <<EOD
    #include <stdio.h>
    void wilma(void)
    {
      printf("this is wilma\n");
    }
    EOD
    
    cat > b2.c <<EOD
    #include <stdio.h>
    void barney(void)
    {
      printf("this is barney from library B\n");
    }
    EOD
    
    cat > main.c <<EOD
    #include <stdio.h>
    #include "a.h"
    int main(void)
    {
      fred();
      barney();
    
      return 0;
    }
    EOD
    
    gcc -Wall -c a1.c -o a1.o
    gcc -Wall -c a2.c -o a2.o
    gcc -Wall -c b1.c -o b1.o
    gcc -Wall -c b2.c -o b2.o
    
    rm -f a.a b.a
    
    # Arrrre you rrrready, mateys?  (This script is not pirated from elsewhere.)
    ar r a.a a1.o
    ar r a.a a2.o
    ar r b.a b1.o
    ar r b.a b2.o
    
    # The following ar optional.
    
    ar -s a.a
    ar -s b.a
    
    gcc -Wall main.c a.a b.a -o main
    
    main
    The output I get:
    Code:
    this is fred
    this is wilma
    this is barney from library A
    I don't think you can keep library A from linking against itself, but you didn't ask for that. :)

    Hope this helps.
    --
    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
  •  
...