Solved

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

Posted on 1998-11-10
7
252 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now