Compiling Library Into Program.

This is a discussion on Compiling Library Into Program. within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I have some memory of you being able to link a library statically if you want to make sure ...

  1. #1
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259

    Compiling Library Into Program.

    Hi,
    I have some memory of you being able to link a library statically if you want to make sure that the program runs on other computers, even if they do not have the library they use installed.

    I have searched google, the forums and the gcc website, but I can't find out how this is done, so I'm simply asking: if this is possible and if it is how you do it?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Read the manual on the linker (ld)
    Read about the "-static" linker option

  3. #3
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    so I should for example use:

    Code:
    ld -llib -static  -o test.o
    ?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    The manual page suggests that -static should be earlier on the command line

  5. #5
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    so

    Code:
    ld -static -o test.o -llib
    ?

    so the "-llib" is in the [file....] in the:
    Code:
    ld [ option ...	] [ file ...  ]
    ?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You are right, but why don't use just link allowing gcc to pass the linker the options, such as this
    Code:
    gcc -static test.c -o test -lblah
    But both work. Read this chapter of advanced linux programming to see how they both work:
    http://www.advancedlinuxprogramming....x-software.pdf
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    When I try this I get the following:

    Code:
    ld: can't locate file for: -lcrt0.o

    I have now made a sarch for "crt" and I found crt1.o and crt2.o but no crt0.o, is this important and how do I fix it?
    Last edited by Shogun; 11-30-2004 at 06:45 AM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Take off the .o in the -l part, like this
    Code:
    gcc -o file file.c -lblah
    /*not*/
    gcc -o file file.c -lblah.o
    Also make sure the library is in the current directory or a system one for libraries, if not, you have to use the -L option to gcc.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  9. #9
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I don't have the .o in the library, I'm not even calling it,no clue where it came from, I search my SDK when I saw the message and I don't have that very library but I have crt1 and crt2...
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  10. #10
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I made some more searching and I found the following:
    guess I can't link stuff statically.. I wounder if there is any other way to do it, I'll have to do some more lookin around...
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Driving the linker directly sucks, there is too much to get right.
    If you really want to know, just compile a small program with the -v option, and see how complex a linker command line can be.

    This works just fine here
    Code:
    $ gcc generic.c && ls -l a.out && file a.out
    -rwxrwxr-x         11586 Nov 30 19:28 a.out
    a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
    $ gcc generic.c -static && ls -l a.out && file a.out
    -rwxrwxr-x        447193 Nov 30 19:28 a.out
    a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
    Obvious to tell that it's statically linked by the sudden jump in the size of the program executable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stray Error while compiling
    By Mankthetank19 in forum C Programming
    Replies: 6
    Last Post: 02-03-2011, 02:24 PM
  2. Help Needed with Compiling C program
    By girishaneja in forum C Programming
    Replies: 2
    Last Post: 05-20-2009, 08:10 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  4. compiling a program that has no configure script
    By serfurj in forum Linux Programming
    Replies: 9
    Last Post: 02-09-2005, 09:53 AM
  5. Help compiling a program
    By jjj93421 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2004, 05:38 PM

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