Solved

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

Posted on 1998-11-10
7
256 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote Change Dates on AIX Automation 7 76
Python Assistance 7 94
Bash Script to Analyze Oracle Schemas 11 125
awk sed variable in file 3 98
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

726 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