473,396 Members | 1,995 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,396 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 14418
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.