473,231 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Looking for a Makefile to create shared lib in Linux

365 100+
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)
Nov 25 '09 #1

✓ answered by RRick

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 14393
mac11
256 100+
I think you need some -Wl flags so the linking is correct. Here's a good reference
http://www.linux.org/docs/ldp/howto/...libraries.html
Nov 25 '09 #2
tvnaidu
365 100+
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
$
Nov 25 '09 #3
mac11
256 100+
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?
Nov 25 '09 #4
tvnaidu
365 100+
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.
Nov 25 '09 #5
tvnaidu
365 100+
I need to link static libs to create a shared lib, any expert can help?
Nov 28 '09 #6
tvnaidu
365 100+
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]#
Nov 30 '09 #7
RRick
463 Expert 256MB
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).
Nov 30 '09 #8
tvnaidu
365 100+
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.
Nov 30 '09 #9
RRick
463 Expert 256MB
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.
Nov 30 '09 #10
tvnaidu
365 100+
Thanks, appreciated. link is very useful.
Nov 30 '09 #11
tvnaidu
365 100+
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'
Dec 1 '09 #12
tvnaidu
365 100+
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
Dec 1 '09 #13
tvnaidu
365 100+
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
Dec 1 '09 #14
tvnaidu
365 100+
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/
Dec 1 '09 #15
tvnaidu
365 100+
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$
Dec 1 '09 #16
RRick
463 Expert 256MB
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.
Dec 2 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim | last post by:
I'm using the shmop_* functions to create and access shared memory. I've never used shared memory before, so I apologize if my reasoning is completely off... $shm_id = shmop_open(ftok(__FILE__,...
0
by: Elephant | last post by:
Hello, question, I want to make a COM-compatible .NET DLL (Visual Basic). For this I need an interface. The DLL has a class that must contain methods that can be used without making an instance...
4
by: Sandeep Chikkerur | last post by:
HI, I am writing a an ATL COM component (a "dll" file). This file calls an executable. That executable is created from the "C" language. (Compiled on Windows platform. Using Microsoft Visual...
1
by: News User | last post by:
This may sound stupid but I am having trouble figuring out this one. Any help would be appreciated. I have 3 classes ClassA & ClassB and ClassC From ClassC I would instantiate ClassA...
3
by: ehaffey | last post by:
Hi, Im trying to get a VB.NET program to create a shared folder. I can make the folder but I cant figure out how to make it as a shared folder. Also I will want to set the permissions on the...
11
by: admin | last post by:
Hi all, First time poster here... I'm a webmaster and I'd like to add a simple script to my website which will allow users to fill in a brief multiple choice questionaire, and then provide a...
1
by: Matt F | last post by:
I need to create a folder in code, and also set sharing to being shared to everyone with full control (or some such variation of security) What I'm doing now is: If Not...
4
by: Travis | last post by:
I'm not a Makefile expert. I have a real small program (just a main.cpp) that needs to tie into an existing library. Now other programs I've seen that use the library just do #include "Library.h"...
1
by: Ron Eggler | last post by:
Hi, I'm running an application on an embedded PC 104 platform on a Linux OS. I need some shared memory and I'm trying to create it with: int prg_shm = ::shmget(0x1999,sizeof(struct...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.