Create a lib from a c program


 
Thread Tools Search this Thread
Top Forums Programming Create a lib from a c program
# 1  
Old 11-04-2002
Create a lib from a c program

I have a function in a c program that I want to to share with other programs.

How do I create a lib using the cc compiler ?
# 2  
Old 11-04-2002
I think that it might be possible to create a shared library in a single step if it contained only one module. But libraries usually contain several modules. Each .c file is compiled seperately. Then they are all combined into a shared library with a single invocation of ld.

But the exact options depend on what os, what release, what compiler, etc.
# 3  
Old 11-04-2002
What I am looking for is the standard c way for creating libs in unix, as standard as possible.

The os I am using is "SunOS 5.6"
# 4  
Old 11-04-2002
this link is for AIX, but I used the procedure to test out on my RHL 7.3 and it works fine... so I think you should not have problem save a little exercise for you to find the appropriate option for your cc implementation...

http://www.faqs.org/faqs/aix-faq/part4/section-1.html

Cheers!
Vishnu.
# 5  
Old 11-04-2002
I'm not sure, but I'll take a guess. I am also assuming that:
you are using /opt/SUNWspro/bin/cc for your compiler.
you are using a 32 bit address space
you are using a sparc chip
but you are not using a sparc v9

If those assumptions are correct, it looks like this might work...

cc -KPIC x.c
cc -KPIC y.c
cc -KPIC z.c
ld -G -Bdynamic -o libxyx.so x.o y.o z.o

I don't have a decent compiler on a 5.6 so I can't test this. But it's close. In your case, you say you have only a single module, so you would have only one cc. But that is very rare with a library. So I'm showing a more normal case where you would combine several modules into a library.

To generalize this more use on many versions on unix, you typically would use a makefile. The "-KPIC" would be in a variable so it could be changed as you moved from one version to another. Ditto for the "-G -Bdynamic".

This link is the Linker and Libraries Guide, which you might want to look at. Also look at the man page for your compiler. Sun has several things called cc so you need to get the MANPATH right or you will be looking at the wrong one.

If this doesn't work, try replacing -KPIC with -Kpic.
# 6  
Old 11-05-2002
Thank you very much for your help, I think I've got it now.

For the record ,this is a simple example of how I did it:

1) Create ut.c with the func to include in the lib

int F1( int i) {
printf("Print Nš = %d\n", i);
}

2) Complie ut.c to create ut.o

cc -c ut.c

3) Create Lib from ut.o

ar r libut.a ut.o

4) Create t1.c to invoke the func in lib

void main ( int argc, char *argv[]) {
int i_in;
if ( argc != 2) {
printf( "Wrong parameters, use : t1 <nš> \n");
return;
}
i_in = atoi( argv[1]);

F1( i_in);
}

5) Complie t1 linking it to the lib created in 3)

cc -o t1 t1.c -L: libut.a

And it Works :
>t1 69
Print Nš = 69
# 7  
Old 01-31-2007
Linker and Libraries Guide -- link not accessible

Hi Perderabo,

I couldn't access the link "Linker and Libraries Guide" given by you.
Don't know if i am alone facing that problem. Could you please check it out and provide me the link..

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a program illustrating SUID

To understand SUID feature, I set SUID bit for a SHELL script. Then I executed the program by a different user. In order to understand how it works, I tried different ways like: 1) I didn't give execute permission for the other user (not an owner) and then he tried to execute it. 2) I... (15 Replies)
Discussion started by: ravisingh
15 Replies

2. Homework & Coursework Questions

create a program that runs two processes linked oven

I need help program in C... :create a program that runs two processes linked oven (1 Reply)
Discussion started by: gizmo16
1 Replies

3. Programming

Can I create a shared object by using an static lib?

for example, I have a static lib name liba.a it offers some interface such a1();a2(); but i do not have the source code of liba.a; If i would like to create a shared object, and offer the similar interface of a1 and a2; Is there a way to fulfill such requirement? thanks. (7 Replies)
Discussion started by: flost
7 Replies

4. Red Hat

ls: /lib/libattr.so.1: no version information available (required by /lib/libacl.so.1)

Hello, I'm experimenting a problem on my rh server. Red Hat Enterprise Linux AS release 3 (Taroon Update 8) 2.4.21-47.ELsmp #1 SMP i686 i686 i386 GNU/Linux It started with a segmentation fault on #id root To resolve it, I've installed coreutils-4.5.3-28.4.i386.rpm But, I... (6 Replies)
Discussion started by: gogol_bordello
6 Replies

5. Shell Programming and Scripting

Shell Program to create a RPM name

Hi Guru,s/Geek,s I need help to create RPM names from rpms. Example : a2ps-4.14-6.fc10.i386 perl-Email-Find-0.10-2.fc10.noarch directfb-1.2.7-2.fc10.i386 libid3tag-0.15.1b-7.fc10.i386 apr-util-1.3.7-1.fc10.i386 libquicktime-1.0.3-4.fc10.i386 The Desired Output is : a2ps... (2 Replies)
Discussion started by: anand.linux1984
2 Replies

6. Programming

create sound using C program

I could not recall the function in C to generate diff type of sounds. Can somebody help me out. (2 Replies)
Discussion started by: bishweshwar
2 Replies

7. Shell Programming and Scripting

create a .dll using cygwin and a .lib

Hi, I inherited a .lib file that I need to use to make a .dll file from a c++ file. I am able to do this in visual studio but I can not do this using cygwin. I would like to build the dll using the commandline in order to create a make file. Can someone help me. I would really appreciate it. ... (0 Replies)
Discussion started by: lsoleyma
0 Replies

8. Programming

How to create a new unix user in through a c program

Hi , I want to create a new user using c program not with unix adduser command . is it possible to write a cprogram to create a new user account , it should accept username , grouid , group name and all other privilages . i can use system calls inside c program to do this . i will... (5 Replies)
Discussion started by: naren_chella
5 Replies

9. Programming

Using a C program to create directories in UNIX

Aloha, I'm attempting to use a C program to create directories and then use a system call to have another program write .dat files into that directory. I understand that I could use the "system("mkdir directory_name")" function however, I would like my program to create a new directory each time... (3 Replies)
Discussion started by: aloha_boi
3 Replies

10. Programming

HOw to load dynamic lib from a statically linked program ?

I need to load a dynamic library from a statically linked program. Is there a way without recompiling my program. when i try to do that my program just crashes. If not possible, how can I avoid crashing the program when i try to load the dynamic lib, again without recompiling. If my... (1 Reply)
Discussion started by: disclaimer
1 Replies
Login or Register to Ask a Question