Thread: How do I make shared libs on Linux?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    How do I make shared libs on Linux?

    Hi,
    I'm building static and shared libs (.a & .sl) with these make options:

    Code:
    SHLIB=libname.sl
    CC=cc
    AR=ld
    ARFLAGS=-Bdynamic -shared -warn-common -warn-constructors -warn-multiple-gp -warn-once -o
    INC=../src
    DEFS=-DPOSIX -DANSI
    CFLAGS=-Wall -Wno-unused -fpic -shared -I. -I$(INC) $(DEFS)
    
    $(SHLIB): $(OBJS)
    	rm -f $(SHLIB)
    	$(AR) $(ARFLAGS) $(SHLIB) $(OBJS)
    
    debug.o: $(SRC)/debug.c $(HEADERS)
    	$(CC) -c $(CFLAGS) $(SRC)/debug.c
    ...
    But after I build my test program and run it, I get a segmentation fault. The program works fine with the static libs.

    On Solaris, the LD_LIBRARY_PATH environment variable is used to specify where the shared libs are. As far as I can tell, Linux uses LIBRARY_PATH. Is that right, or do I need something else?

    Do I need any other link options, or should I get rid of any that I have...?

    This is how my test program is built:

    Code:
    CC=cc
    CFLAGS=-Wall -DPOSIX -DANSI
    SHLIBS=-L$$SHLIB_PATH
    
    ivpslibs: scp.o rproc.o sstubs.o smain.o
    	$(CC) -shared -o ivpslibs scp.o rproc.o sstubs.o smain.o $(SHLIBS)
    
    sstubs.o: sstubs.c ivp.h
    	$(CC) $(CFLAGS) -c $(INCS) sstubs.c
    ...
    I don't know if it's a problem with how I build the shared lib or the test program or both?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    On Solaris, the LD_LIBRARY_PATH environment variable is used to specify where the shared libs are. As far as I can tell, Linux uses LIBRARY_PATH. Is that right, or do I need something else?
    No, it is also LD_LIBRARY_PATH.

    You don't need "-shared" in your CFLAGS. Also, why name the library with .sl instead of .so? The dynamic linker is probably confused by that.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You do not create shared libraries with "ar" - you create them with gcc or ld - I'm not quite sure about which switches you throw to make it create a .so file [which is the correct name, by the way].

    If you want statically link with a .a file (which is the normal output name from a "ar" built archive) that would be fine too - but you need to specify -lname to make that happen in that case - you are only specifying -L<somepath> which tells the linker (as called by gcc) to look for libraries in this path, not actually to include any of the libraries that may be in there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    You do not create shared libraries with "ar" - you create them with gcc or ld - I'm not quite sure about which switches you throw to make it create a .so file [which is the correct name, by the way].
    He IS using ld, he just sticks it in a variable called $(AR). I thought it was weird but didn't say anything.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    He IS using ld, he just sticks it in a variable called $(AR). I thought it was weird but didn't say anything.
    Ah, ok, so it's just the two things then: strange name (which may be OK, but it's hardly helping) and not linking in the actual library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK thanks, I'll try those suggestions.

    The .sl looked weird to me too. I ported this makefile from HPUX...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  2. Port app from Windows to Linux
    By BobS0327 in forum Linux Programming
    Replies: 12
    Last Post: 02-12-2006, 02:35 AM
  3. libs under linux
    By moonlord in forum Linux Programming
    Replies: 1
    Last Post: 08-22-2005, 09:26 AM
  4. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM