Looking for simple Makefile to make shared lib (.so) in Linux (I have two C files file1.c and file2.c, also two static libs slib1.a and slib2.a), can I get Makefile to compile all of them to crate final.so shared lib?.
I wrote this to compile using GNU compiler in Linux, this is not working, thanks in advance.
CC = gcc
AR = ar rc
CFLAGS = -Wall -g -O2 -I. -Iinclude -I../common/include -fPIC
OBJECTS = file1.o file2.o
SHARED_LIB = final.so
LIBS = -lslib1 -lslib2 -lpthread
$(SHARED_LIB): $(OBJECTS)
$(CC) $(SHARED_LIB) $(OBJECTS) $(LIBS)
%.o : %.c
$(CC) $(CFLAGS) -c $<
clean:
$(RM) $(OBJECTS)
$(RM) $(SHARED_LIB)
It looks like you have a couple of issues still outstanding.
It sounds like you were able to link in the static libraries to the shared library. Like Mac11 said, if you want to use the -l option, then the library must have a specific format. With the -l option, you also use the -L option to define the directory path.
You can also specify the library directly (dirPath/slib1.a) without the -l option. This is sometimes handy.
Ldd only shows the shared libraries used by the program or shared library. Since libHashTable is a static library, only the object code needed by the shared library is extracted from the static library. If you want to see what was pulled in from libHashTable.a, try "nm yourSharedLib.so". The nm command will list out all of the object code routines, but only if the symbol information was compiled into the static library. That's why you want some level of debug info (i.e. -g option) when compiling, even if it is just level 1 (i.e. -g1 option).
16 14173
Thanks.
I have two static libs slib1.a and slib2.a in other directory, but I copied locally to create .so lib first, but I am getting error when it tries to create .so lib. any idea?
$ gcc -Wall -g -O2 -I. -Iinclude -fPIC -c file1.c
$ gcc -Wall -g -O2 -I. -Iinclude -fPIC -c file2.c
$ gcc -shared -lslib1 -lslib2 -Wl,-soname,libfinal.so.1 -o libfinal.so.1.0.1 .o file1.0 file2.o -lpthread
/usr/bin/ld: cannot find -lslib1
collect2: ld returned 1 exit status
$
When you do -lslib1 ld is looking for "libslib.a" and can't find it. (see your gcc manpage or google about the -l option). Maybe you can just rename your slib1.a to libslib1.a?
I renamed it, it didn't help. I can see lot of questions on google, but no answers at all. I am scratching my head what to do.
I need to link static libs to create a shared lib, any expert can help?
I made shared lib called libmanager.so by linking static lib libHashTable and pthread, when I do "ldd libmanager.so", It won't show that hashtable?.
$gcc -Wall -g -O2 -I. -Iinclude -c -fPIC manager.c
$gcc -shared -Wl,-soname,libmanager.so.1 -o libmanager.so.1.0 -L/opt/lib -lHashTable -lpthread manager.o
$mv libmanager.so.1.0 /opt/lib
$ln -sf /opt/lib/libmanager.so.1.0 /opt/lib/libmanager.so
$ln -sf /opt/lib/libmanager.so.1.0 /opt/lib/libmanager.so.1
localhost lib]# ls
libHashTable.a
libmanager.so -> /opt/lib/libmanager.so.1.0
libmanager.so.1 -> /opt/lib/libmanager.so.1.0
libscoreMgr.so.1.0
localhost lib]# ldd libmanager.so
libpthread.so.0 => /lib/tls/libpthread.so.0 (0xb75d9000)
libc.so.6 => /lib/tls/libc.so.6 (0xb74a2000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
localhost lib]#
It looks like you have a couple of issues still outstanding.
It sounds like you were able to link in the static libraries to the shared library. Like Mac11 said, if you want to use the -l option, then the library must have a specific format. With the -l option, you also use the -L option to define the directory path.
You can also specify the library directly (dirPath/slib1.a) without the -l option. This is sometimes handy.
Ldd only shows the shared libraries used by the program or shared library. Since libHashTable is a static library, only the object code needed by the shared library is extracted from the static library. If you want to see what was pulled in from libHashTable.a, try "nm yourSharedLib.so". The nm command will list out all of the object code routines, but only if the symbol information was compiled into the static library. That's why you want some level of debug info (i.e. -g option) when compiling, even if it is just level 1 (i.e. -g1 option).
Thansk Rick I did nm xyz.so, I can see the symbol of Hash. I can use nm and ldd commands for shared libs to findout dependencies also linked files. I am refering Linux docs, incase if any useful commands to debug shared libs, can I get it. thanks in advance.
Nm and ldd are the main commands for viewing program internals.
strace is real handy when running a program and seeing what is happening at the system or io level.
objdump displays all kinds of things for object code files and libraries.
This article has more information. http://www.linuxjournal.com/article/7330. The strings command looks interesting.
Thanks, appreciated. link is very useful.
when I do nm on one of the shared lib below, I can see like this all HastTable functions (ht_...), but when I do final linking, I am getting "undefined reference" error, does nm means it doesn't link actual functions, may be nm shows just calling functions only, when I do nm on shared lib, I can see those function calls with "U", means undefined right?
localhost lib]$ nm libmanager.so | grep ht_
U ht_create
U ht_delete
U ht_destroy
U ht_find
U ht_insert
$
final linking error:
/opt/lib/libmanager.so: undefined reference to `ht_delete'
/opt/lib/libmanager.so: undefined reference to `ht_destroy'
/opt/lib/libmanager.so: undefined reference to `ht_insert'
I did nm and redirected output to a file, then I seached for ht_ function, I found each one at one place only:
000025f4 A _GLOBAL_OFFSET_TABLE_
w __gmon_start__
U htCalloc
U ht_create
U ht_delete
U ht_destroy
U ht_find
U ht_insert
00000af4 T _init
Looklike when shared lib gets created, it is not linking static lib which I passing with -l option below (I have static lib file in /opt/lib directory), when I do nm on shared lib, it doesn't know where the code is:
$gcc -shared -Wl,-soname,libmanager.so.1 -o libmanager.so.1.0 -L/opt/lib -lHashTable -lpthread manager.o
I was referring this IBM link about shared libs, when I issue ldd command, it show all libraries info, the above ldd command doesn't show hashtable, but according to below link ldd command, it show show. http://www.ibm.com/developerworks/library/l-shobj/
if I do nm on static lib HashTable, I can see all text marked with "T", I linked this to create shared lib, when I do nm on shared lib, I gets "U" for those symbols, also for final exe file, I gets "undefined reference".
localhost HashTable]$ nm libHashTable.a
hash.o:
U free
U _GLOBAL_OFFSET_TABLE_
00000000 T ht_create
00000270 T ht_delete
0000009c T ht_destroy
00000220 T ht_find
00000128 T ht_insert
00000318 T ht_process
U malloc
localhost HashTab$
I assume the situation is: When you try to create the manager shared library, you are getting the unresolved references. This is exactly what would happen if the gcc linker can't find them. Like an executable, a shared library must resolve all references during linking.
You can double check the paths and library name again, but if the gcc linker couldn't find the HashTable library, it would have complained about it.
Was the hash table library compiled by another compiler? In C++, the name mangling is not standardized between compilers. It might be time to recompile the hash table locally. Then you would really know what is going on.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by Jim |
last post: by
|
reply
views
Thread by Elephant |
last post: by
|
4 posts
views
Thread by Sandeep Chikkerur |
last post: by
|
1 post
views
Thread by News User |
last post: by
|
3 posts
views
Thread by ehaffey |
last post: by
|
11 posts
views
Thread by admin |
last post: by
|
1 post
views
Thread by Matt F |
last post: by
|
4 posts
views
Thread by Travis |
last post: by
|
1 post
views
Thread by Ron Eggler |
last post: by
| | | | | | | | | | |