?
Solved

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

Posted on 1998-11-10
7
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
jos010697 earned 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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.
Suggested Courses

777 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