LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Link one library statically others dynamically (gcc, g++) (https://www.linuxquestions.org/questions/programming-9/link-one-library-statically-others-dynamically-gcc-g-890217/)

czappa 07-06-2011 07:57 AM

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 );
}

paulsm4 07-07-2011 02:41 AM

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

czappa 07-07-2011 07:34 AM

Quote:

Originally Posted by paulsm4 (Post 4407549)
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

paulsm4 07-07-2011 07:15 PM

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" :(

ta0kira 07-07-2011 11:32 PM

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

rwstoneback 03-22-2012 07:51 AM

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.

dwhitney67 03-22-2012 08:36 AM

Quote:

Originally Posted by rwstoneback (Post 4633529)
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).

rwstoneback 03-22-2012 08:47 AM

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.


All times are GMT -5. The time now is 12:43 PM.