Solved

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

Posted on 1998-11-10
7
258 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 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 Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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 Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

695 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