LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2011, 04:13 AM   #1
Last_Winter
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
Make static library linked with dynamic mysql lib


Hi to all,

I need to make a static library linked with the dynamic mysql library.

I'm using gcc 3.3.6 on HP-UX 11.11 64 bit.

I'm trying to create an object without linking mysqllib:

gcc -c ./richieste_cdi.c -I/mydir/mysql/include -o ./richi.o

Then:
gcc -static ./richi.o -L/dbserv/CDIS/lib -lmysqlclient -o ./librich.a

Result:
/usr/ccs/bin/ld: Can't find library: "mysqlclient"
collect2: ld returned 1 exit status

I know that this is wrong because i have to use ar to create a static library....but how to link with mysql lib?????

Thank you.
 
Old 07-29-2011, 01:04 PM   #2
tinyTux
Member
 
Registered: Mar 2011
Location: Extended Memory
Distribution: Gentoo
Posts: 64

Rep: Reputation: 9
My C is rather rusty, but I think you are a bit confused about how all this works.

First of all, the -static switch doesn't create static libraries. From GCC(1):

Code:
       -static
           On systems that support dynamic linking, this prevents linking with the shared libraries.
           On other systems, this option has no effect.
In other words, -static prevents the compiler from doing any dynamic linking. So obviously, even if your system could find mysqlclient library, you still wouldn't be able to link to it.

Second, if you are trying to create a static library (.a archive) you won't be dynamically linking to any library. That's up to the person who eventually compiles a binary. For example, he might want to compile in your library statically but also compile in the mysqlclient library statically. You can't decide that for him. Here's one tutorial on creating static libraries:

http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

Here is another article on creating binaries with mixed static/dynamic linking:

http://frigidcode.com/documentation/...c-linking.html

Thirdly, even if you did need to dynamically link to libmysqlclient, the reason is clear from the error message: the compiler can't find it. Probably it is installed in some place outside your normal library path. It's not installed on my system right now, but one google link suggested it would be in the /usr/lib/mysql directory. However, if you need more help with dynamic linking, just post some more and let us know.
 
Old 08-06-2011, 07:13 AM   #3
Last_Winter
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for replay, here's my solution:

gcc -fpic -c ./mylib.c -I/opt/iexpress/mysql/include/mysql -o ./mylib.o
ld -L/opt/iexpress/mysql/lib/mysql -lmysqlclient -L/opt/openssl/lib/ -lssl -lcrypto -b ./mylib.o -o ./libmy.a
 
Old 08-06-2011, 10:15 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Last_Winter View Post
I know that this is wrong because i have to use ar to create a static library....but how to link with mysql lib?????
Quote:
Originally Posted by Last_Winter View Post
Thanks for replay, here's my solution:

gcc -fpic -c ./mylib.c -I/opt/iexpress/mysql/include/mysql -o ./mylib.o
ld -L/opt/iexpress/mysql/lib/mysql -lmysqlclient -L/opt/openssl/lib/ -lssl -lcrypto -b ./mylib.o -o ./libmy.a
Static libraries aren't linked; they're literally just archives of .o files. This means that although ./libmy.a ends in ".a" it isn't a static library. At the point of linking you need to link in the dependencies of the static libraries you use, and since static libraries aren't linked, that means whoever uses your library must explicitly link to -lmysqlclient.
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the difference between static and dynamically linked library... trist007 Linux - Newbie 5 09-19-2010 08:40 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 01:24 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration