Solved

How to create a dynamic lib with c on HP/unix

Posted on 1998-11-10
7
255 Views
Last Modified: 2013-12-06
How to create a dynamic lib with c on HP/unix.
1. The rule to write the source files?
2. How to create it?
3. How to call function in a dynamic lib?
  Example will be thankful!
0
Comment
Question by:wpy
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 2007743
It depends a bit on what compiler and linker you're using; assuming you're
using gcc and the GNU linker, it's quite easy:

1: compile all your .c files using the -fPIC flag; this create Position Independent Code
2: link all your .o files using the -shared and -shared and -export-dynamic flags, or, if
   you've    created a .a archive, use the -whole-archive flag to tell the linker that you
   want the entire archive to be linked in; name the output file <something>.so
3: you can simply link any other object file against this .so file. The linker is smart
   enough to know (by looking at the .so extension and the magic number) that
   you want runtime linking;

An alternative to step 3 is, to explicitely include the runtime linker in your executable;
it would look something like this:

typedef double (*math_func)(double);

int main() {
   void* sofile= dlopen("libm.so");
   math_func cosine= dlsym(sofile, "cos");

   printf("cosine(%f)= %f\n", 0.0, cosine(0.0);

   return 0;
}

kind regards,

Jos aka jos@and.nl


0
 

Author Comment

by:wpy
ID: 2007744
Thanks . but I want  a HP/Unix version
0
 
LVL 4

Expert Comment

by:jos010697
ID: 2007745
If you don't have the GNU stuff available, basically all you have to do is:

$ cc -Aa -c +z length.c volume.c mass.c
$ ld -b -o libunits.sl length.o volume.o mass.o

the two commands above compile the translation units lenght.c, volume.c
and mass.c. All three .o files are combined into a shared library libunits.sl

I stole this example from:

http://wsspinfo.cern.ch/file/man.html

One of the links of from this page leads to HP/UX specific stuff ...

kind regards,

Jos aka jos@and.nl

ps. I sincerely _hate_ all those different linker options ...


0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:wpy
ID: 2007746
Thanks very much!
0
 

Author Comment

by:wpy
ID: 2007747
Thanks very much!
Have you any sample of shl_load(3X) and the relative functions on HP/Unix?
Please help!
0
 
LVL 4

Expert Comment

by:jos010697
ID: 2007748
Ok, here goes:

#include <stdio.h>
#include <dl.h>

/* a function pointer type: taking one double arg, returning a double:  */
typedef double (*funcp_t)(double);

int main() {

shl_t   handle;      /* handle to the shared archive */
funcp_t cosine;    /* pointer to a function */

/* try to open the math archive */
handle= shl_load("/usr/lib/libm", BIND_IMMEDIATE);

if (!handle) {
        perror("can't find lib");
        exit(1);
}

/* try to find the 'cos' symbol */
if (shl_findsym(handle, "cos", TYPE_PROCEDURE, &cosine) != 0) {
        perror("can't find symbol");
        exit(1);
}

/* do something with the 'cos' symbol: */
printf("cosine(1.0)= %f\n", cosine(1.0));

/* and detach the archive again */
shl_unload(handle);

return 0;

}

This is from the top of my head, (and totally untested), by I think you get the
picture ...

kind regards,

Jos aka jos@and.nl
0
 

Author Comment

by:wpy
ID: 2007749
Thanks very much! I'll test it.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question