LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-06-2011, 07:57 AM   #1
czappa
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
Link one library statically others dynamically (gcc, g++)


Hi everyone!


I would like to make gcc to link one lib (pthread in the example) statically, and the rest dynamically.
(See the example .c file at the end of this post.)

> gcc main.c -lpthread
compiles fine, but every lib is linked dynamically.

> gcc main.c -static -lpthread
Now every lib is linked statically
> ldd a.out
not a dynamic executable


I would like to link the libpthread statically and the rest dynamically.
Is it possible?

http://www.network-theory.co.uk/docs...cintro_25.html says if I give the full path to the .a lib then it will be linked statically:
> gcc main.c /usr/lib/libpthread.a
/usr/lib/libpthread.a(pthread_create.o): In function `setxid_signal_thread':
(.text+0xce): undefined reference to `_dl_sysinfo'
/usr/lib/libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x189): undefined reference to `_dl_sysinfo'
/usr/lib/libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x1a1): undefined reference to `_dl_sysinfo'
/usr/lib/libpthread.a(pthread_create.o): In function `start_thread':
(.text+0x6df): undefined reference to `_dl_sysinfo'
(...)
So for me it does not work.

On other sites I found that I should use -Wl,-Bstatic -lpthread -Wl,-Bdynamic, but it also doesn't work:
>gcc main.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `setxid_signal_thread':
(.text+0xce): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x189): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x1a1): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `start_thread':
(.text+0x6df): undefined reference to `_dl_sysinfo'
(...)

So, how can I do that?

Machine:
Linux 2.6.16.60-0.27-bigsmp #1 SMP Mon Jul 28 13:06:32 UTC 2008 i686 i686 i386 GNU/Linux
SLED 10

Thanks in advance,
czappa



p.s.:
As an example I used a little demo program from http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html


Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

main()
{
int rc1, rc2;
pthread_t thread1, thread2;

/* Create independent threads each of which will execute functionC */

if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}

if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}

/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */

pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

exit(0);
}

void *functionC()

{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}
 
Old 07-07-2011, 02:41 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. The "-Wl,-Bstatic SOMELIBS -Wl,-Bdynamic OTHERLIBS" syntax is definitely the way to go.

2. Unfortunately, it sounds like ld "loses track" after your -Bstatic, and you need to explicitly specify some (or all) of the other dynamic libraries you need.

SUGGESTION:
Start here, and see how much (if at all) further it gets you:
Code:
gcc -g -Wall -pedantic -o myprog main.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -ldl -lc
'Hope that helps .. PSM
 
1 members found this post helpful.
Old 07-07-2011, 07:34 AM   #3
czappa
LQ Newbie
 
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
Hi -

1. The "-Wl,-Bstatic SOMELIBS -Wl,-Bdynamic OTHERLIBS" syntax is definitely the way to go.

2. Unfortunately, it sounds like ld "loses track" after your -Bstatic, and you need to explicitly specify some (or all) of the other dynamic libraries you need.

SUGGESTION:
Start here, and see how much (if at all) further it gets you:
Code:
gcc -g -Wall -pedantic -o myprog main.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -ldl -lc
'Hope that helps .. PSM
Hi,

Thank you for your reply.
I tried what you advised, but the result is the same as before:

> gcc -g -Wall -pedantic -o myprog main.c -Wl,-Bstatic -lpthread -Wl,-Bdynamic -ldl -lc
main.c:10: warning: return type defaults to 'int'
main.c: In function 'functionC':
main.c:42: warning: control reaches end of non-void function
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `setxid_signal_thread':
(.text+0xce): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x189): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `__nptl_setxid':
(.text+0x1a1): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `start_thread':
(.text+0x6df): undefined reference to `_dl_sysinfo'
/usr/lib/gcc/i586-suse-linux/4.1.2/../../../libpthread.a(pthread_create.o): In function `start_thread':
(.text+0x7a1): undefined reference to `_dl_sysinfo'
...


One thing to mention:
I have tried yesterday the "-Wl,-Bstatic SOMELIBS -Wl,-Bdynamic OTHERLIBS" on my home computer (Slackware 13.37 64bit) with the same result.


Regards,
czappa
 
Old 07-07-2011, 07:15 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sorry

For whatever it's worth, "dl_sysinfo" should be in "libc.a" and/or "libc.so".

So you should have picked it up when you added an explicitly "-lc"
 
Old 07-07-2011, 11:32 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Incremental linking seems to help a little; however, there is still an error:
Code:
gcc -g -Wall -pedantic -fPIE -c main.c
gcc -nostdlib main.o -o main2.o -Wl,-r,-Bstatic -lpthread -Wl,-Bdynamic -ldl -lc
gcc -Wl,-pie main2.o -o myprog
Code:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: relocation R_X86_64_32S against `__libc_csu_fini' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
If -fPIE is replaced with -fPIC and -pie is removed it gives an error that -fPIE an -pie are required for a libc function. I'm sure there's a way to split this into an .so and an executable. You might just wrap the pthread calls and create a statically-linked .so with only those functions, then link main.c to that.

I know I've tried to do something like this before, and I probably posted a thread a few years ago about it, but I ended up giving up on the idea.
Kevin Barry

Last edited by ta0kira; 07-07-2011 at 11:36 PM.
 
1 members found this post helpful.
Old 03-22-2012, 07:51 AM   #6
rwstoneback
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: Disabled
Late Answer - But works for me

I know this is very late, but I was having the same issue trying to link statically to some libs and dynamically to others. I could never get -Wl to work right. However, I did find a way to do it that worked for me.

This site gave me all the answers: https://frigidcode.com/articles/gcc-...-linking.shtml

Basically, pass the libraries that you want to link dynamically with to the command just as you would normally. Then the files that you need to link statically, pass the path to the .a file of the library that you wish to use.
 
Old 03-22-2012, 08:36 AM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,539

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by rwstoneback View Post
Basically, pass the libraries that you want to link dynamically with to the command just as you would normally. Then the files that you need to link statically, pass the path to the .a file of the library that you wish to use.
You should not have had to specify the full path. And taking advice from a blog, where there is not an option for anyone to refute what the blogger stated is, IMHO, unwise. Suffice to say, the blogger is wrong (too).
 
Old 03-22-2012, 08:47 AM   #8
rwstoneback
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: Disabled
yea

All I was trying to say was that I had an answer that worked for me after many frustrating hours of trying to figure it out using -Wl, based on ideas in this forum.

Ya know, just someone going out of their way to sign up so I can post an answer that might help someone down the line. But apparently, this is not welcomed here. IMHO, if an answer from a blog solves my problem, I could care less whether it allows comments/refutes. Following your logic, I would have ignored all his useful information just because I didn't like the fact that people can't reply.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to dynamically link a library? PinoyAko Programming 4 05-30-2011 05:27 AM
Two problems about gcc link share library. JerryMega Linux - Software 2 12-12-2008 02:20 AM
How To Statically Link to a Library Using g++? jonwatson Programming 4 07-23-2008 07:37 AM
[gcc] how to statically link my program? G00fy Programming 5 04-16-2006 04:35 PM
How to link a library to gcc? Andrea_81 Programming 6 05-05-2005 04:22 AM

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

All times are GMT -5. The time now is 06:32 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