compiling shared library

This is a discussion on compiling shared library within the C Programming forums, part of the General Programming Boards category; I am trying to compile a program that uses a shared library. I am using the MinGW compiler. My library ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    compiling shared library

    I am trying to compile a program that uses a shared library. I am using the MinGW compiler. My library file is called libtest.c and I have compiled it into a libary using these commands:
    gcc -c -fpic ..\source\libtest.c
    gcc -shared -o libtest.so libtest.o
    I get a libtest.so file so I am assuming it worked well.

    Now I try to compile the file containing the program (called mainprog.c) using this command:
    gcc -I..\bin -L..\bin -o mainprog ..\source\mainprog.c -ltest

    then the compiler complains it cannot find -ltest

    I am calling the compiler from within the bin-directory, so it should be pointed right.
    I am sure it is somehting stupid, but I am getting blind for it I guess.
    Any help would be appreciated.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,499
    I'm not sure, but
    Is libtest.so equivalent to a library named test as seen by the linker?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by manasij7479 View Post
    I'm not sure, but
    Is libtest.so equivalent to a library named test as seen by the linker?
    I am using this tutorial and that says so that you should leave out the prefix 'lib'

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,499
    Quote Originally Posted by django View Post
    I am using this tutorial and that says so that you should leave out the prefix 'lib'
    Sorry..
    btw,.. I followed the tutorial and found expected results (though on linux).

    So I'll take another guess and say that your assertion that:
    "I am calling the compiler from within the bin-directory, so it should be pointed right."
    could be wrong.

    Also, did you move the
    .h file to /include
    and the
    .so file to /lib
    ?
    Last edited by manasij7479; 08-24-2011 at 03:16 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Can you provide us the sources so we can compile it ourselves?

    Also, since this is giving you trouble, are you against compiling it statically?

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by manasij7479 View Post
    So I'll take another guess and say that your assertion that:
    "I am calling the compiler from within the bin-directory, so it should be pointed right."
    could be wrong.
    Yes, it seems so, but it beats me where it points to then.
    Quote Originally Posted by manasij7479 View Post
    Also, did you move the
    .h file to /include
    and the
    .so file to /lib
    ?
    No, but should I, the -I and the -L options both point to the bin directory where both the .so file and the .h file are.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by carrotcake1029 View Post
    Can you provide us the sources so we can compile it ourselves?

    Also, since this is giving you trouble, are you against compiling it statically?
    Yes, I succeed in compiling it statically with the following commands:
    gcc -c ..\source\libtest.c
    ar rs libtest.a libtest.o
    gcc --static -I..\bin -L..\bin -o mainprog ..\source\mainprog.c -ltest

    The sourcecode:
    Code:
    libtest.c:
    --------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include "libtest.h"
    
    void yess() {
    	printf("How sweet is success");
    }
    
    libtest.h
    ---------
    #include <stdio.h>
    #include <stdlib.h>
    
    extern void yess(void);
    
    mainprog.c
    ----------
    #include <stdio.h>
    #include <stdlib.h>
    #include "libtest.h"
    
    int main() {
    	yess();	
    	return(0);
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    -ltest means "I have a library called libtest.a". If you do not have such a library, well, that's not going to work so well.

    I got it to work by just putting "libtest.so" on the command line; I don't know enough about gcc+MinGW+Windows to know whether that's actually a run-time link or a static link. Will test it out in a bit.

    EDIT: Tests (namely changing the def of yess() and recompiling only the .so) indicate that it is runtime (dynamic) linking.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by tabstop View Post
    -ltest means "I have a library called libtest.a". If you do not have such a library, well, that's not going to work so well.
    I do have libtest.a in the directory bin.

    Quote Originally Posted by tabstop View Post
    I got it to work by just putting "libtest.so" on the command line; I don't know enough about gcc+MinGW+Windows to know whether that's actually a run-time link or a static link. Will test it out in a bit.
    Could you state the complete command?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    gcc -o mainprog.exe mainprog.c libtest.so

    (ETA: You have a libtest.a now, because you made it as a static library a bit later -- but you probably didn't have such a thing at the beginning, and you probably don't want to use it since it is a static library (unless that's not actually important).)
    Last edited by tabstop; 08-24-2011 at 04:01 PM.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by django View Post
    I am trying to compile a program that uses a shared library. I am using the MinGW compiler. My library file is called libtest.c and I have compiled it into a libary using these commands:
    gcc -c -fpic ..\source\libtest.c
    gcc -shared -o libtest.so libtest.o
    I get a libtest.so file so I am assuming it worked well.
    IIRC, the fpic switch needs to be supplied to the second gcc command also (shown in red):
    Code:
    gcc -fpic -shared -o libtest.so libtest.o

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by tabstop View Post
    -ltest means "I have a library called libtest.a". If you do not have such a library, well, that's not going to work so well.

    I got it to work by just putting "libtest.so" on the command line; I don't know enough about gcc+MinGW+Windows to know whether that's actually a run-time link or a static link. Will test it out in a bit.

    EDIT: Tests (namely changing the def of yess() and recompiling only the .so) indicate that it is runtime (dynamic) linking.
    Great it works. If I compile, it works, if I then delete the .so file, it does not work anymore because it can't find the library (which is what you want with a dynamic library)! Great! Thanks! Hallelujah!

    I don't know if the tutorial is broken, or if this is windows specific behavious. Anyway, the guy states that for compiling with a dynamic library you can use
    gcc -I../include -L../lib -o printer printer.c -llprprint
    where lprprint refers to liblprprint.so, so according to him it does not refer to .a files only.
    Anyway, I shouldn't probably use linux tutorials, even if the MinGW is said to keep stuff compatible to the unix gcc compiler.
    Thanks again!
    Last edited by django; 08-24-2011 at 04:20 PM.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by itCbitC View Post
    IIRC, the fpic switch needs to be supplied to the second gcc command also (shown in red):
    Code:
    gcc -fpic -shared -o libtest.so libtest.o
    Hm are you sure, the compiler now already warns:
    -fpic is ignored for target <all code is position independent>
    Anyway, I believe it has something to do with where it is placed in memory, but I do not get it truly right now.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    @itC: That is true. OTOH, it is windows, and gcc complains when you use -fpic because "all code is position independent".

    @django: Using -l for a .so file doesn't work in Linux either (or at least not in my Ubuntu), unless you specify the path (maybe you did).

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,499
    Quote Originally Posted by tabstop View Post
    @django: Using -l for a .so file doesn't work in Linux either (or at least not in my Ubuntu).
    You need to run ldconfig for that.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling A Shared library
    By Ectara in forum C Programming
    Replies: 6
    Last Post: 01-18-2010, 04:39 PM
  2. shared library for QNX
    By ReeV in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 11:58 AM
  3. GCC: Compiling with both static and shared libraries
    By eatwithaspork in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 02:48 PM
  4. error on using shared library
    By -EquinoX- in forum Tech Board
    Replies: 2
    Last Post: 05-06-2008, 05:42 PM
  5. Shared Library
    By Kinasz in forum Linux Programming
    Replies: 5
    Last Post: 10-07-2002, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21