Question about gcc linking with dependent dynamic libraries
Question about gcc linking with dependent dynamic libraries
- Subject: Question about gcc linking with dependent dynamic libraries
- From: Rustam Muginov <email@hidden>
- Date: Fri, 10 Nov 2006 17:15:50 +0300
Hello all.
I have got a question about how gcc link while using dependent dynamic
libraries.
Here is my test environment:
Source files: (total 3 files)
low.c:
extern int low_level(int a, int b);
int low_level(int a, int b)
{
return a+b;
}
high.c:
extern int low_level(int a, int b);
extern int high_level(int a, int b, int c);
int high_level(int a, int b, int c)
{
return low_level(a, b) + c;
}
test c:
#include <stdio.h>
extern int high_level(int a, int b, int c);
int main()
{
int res = high_level(100, 10, 1);
printf( "%d\n", res );
return 0;
}
Compilation the test: (current directory is where all files are
created, i am omiting "@executable_path" key for the simplicity, e.t.c.
1) Compiling the low-level library
gcc -O3 -o lowlevel.dylib -dynamiclib low.c
It gives the shared library which exports one symbol, otool output is:
mac:~/Desktop/test muginov$ otool -TV lowlevel.dylib
lowlevel.dylib:
Table of contents (1 entries)
module name symbol name
ccgHe3N3.o _low_level
mac:~/Desktop/test muginov$
2) Compiling the high-level library
gcc -O3 -o highlevel.dylib -dynamiclib high.c lowlevel.dylib
It gives the shared library with imports one symbol from the
"lowlevel.dylib" and exports one symbol, otool output is:
mac:~/Desktop/test muginov$ otool -TV highlevel.dylib
highlevel.dylib:
Table of contents (1 entries)
module name symbol name
ccOAj34X.o _high_level
mac:~/Desktop/test muginov$
3) Compiling the test application which imports the function from the
high-level library
gcc -O3 -o test test.c highlevel.dylib
This gives the executable which is linked with the high-level library
and runs as expected:
mac:~/Desktop/test muginov$ otool -L test
test:
highlevel.dylib (compatibility version 0.0.0, current version
0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0,
current version 71.1.4)
mac:~/Desktop/test muginov$ ./test
111
mac:~/Desktop/test muginov$
At this point, everything works as expected.
But if i try to remove the low-level library
rm lowlevel.dylib
and then try to rebuild the test (which links against the high-level
library), linker complaining that he can not find low-level library;
mac:~/Desktop/test muginov$ gcc -O3 -o test test.c highlevel.dylib
ld: warning can't open dynamic library: lowlevel.dylib (checking for
undefined symbols may be affected) (No such file or directory, errno =
2)
ld: Undefined symbols:
_low_level referenced from highlevel expected to be defined in
lowlevel.dylib
mac:~/Desktop/test muginov$
I do understand that low-level library is required to launch the
application, but why do linker try to resolve the dependency of higher
level library on lower level library during the build time?
Are where any ways to change this behaviour?
I am using OS X 10.3.9 XCode 1.5 gcc version 3.3
Thank you in advance.
--
Sincerely,
Rustam Muginov
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden