C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-29-2004, 06:54 AM   #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.
Shogun is offline   Reply With Quote
Old 11-29-2004, 06:58 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,630
Read the manual on the linker (ld)
Read about the "-static" linker option
Salem is offline   Reply With Quote
Old 11-29-2004, 07:14 AM   #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.
Shogun is offline   Reply With Quote
Old 11-29-2004, 07:37 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,630
The manual page suggests that -static should be earlier on the command line
Salem is offline   Reply With Quote
Old 11-29-2004, 07:43 AM   #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.
Shogun is offline   Reply With Quote
Old 11-29-2004, 09:30 AM   #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
chrismiceli is offline   Reply With Quote
Old 11-30-2004, 07:41 AM   #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?
__________________
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.

Last edited by Shogun; 11-30-2004 at 07:45 AM.
Shogun is offline   Reply With Quote
Old 11-30-2004, 09:21 AM   #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
chrismiceli is offline   Reply With Quote
Old 11-30-2004, 09:24 AM   #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.
Shogun is offline   Reply With Quote
Old 11-30-2004, 01:21 PM   #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.
Shogun is offline   Reply With Quote
Old 11-30-2004, 01:29 PM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,630
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.
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed with Compiling C program girishaneja C Programming 2 05-20-2009 08:10 AM
Client-server system with input from separate program robot-ic Networking/Device Communication 3 01-16-2009 03:30 PM
Stray Error while compiling Mankthetank19 C Programming 4 12-02-2008 05:55 AM
compiling a program that has no configure script serfurj Linux Programming 9 02-09-2005 10:53 AM
Help compiling a program jjj93421 C++ Programming 1 03-08-2004 06:38 PM


All times are GMT -6. The time now is 09:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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