How do I write a shared/static library?
follow is my code:
" shared.c " - It is my shared library
#include <stdio.h>
void fun1(void )
{
printf(“welcome to shared library\n” );
}
int fun2(int a , int b )
{
printf(“a+b=%d\n”,(a+b) );
return (a+b);
}
my main code:
#include <stdio.h>
int main( void )
{
int sum;
fun1();
sum=fun2(2,6);
printf(“sum=%d\n”,sum);
return EXIT_SUCCESS;
}
but there are some error when I build it.
*** [C:/QNX630/workex/get-local_time/x86/o/get-local_time] Error 1
*** [C:/QNX630/workex/get-local_time/x86/o-g/get-local_time_g] Error get-local_time.o * In function main': undefined reference to
fun1’
get-local_time.o * undefined reference to fun1' undefined reference to
fun2’
undefined reference to fun1' undefined reference to
fun2’
$gcc -fpic -c shared.c
$ gcc -shared -o libshared.so shared.o
$ gcc hello.c libshared.so
$ ./a.out
If I use the comm on the command line , the shared library is normal.
But I want to use QNX IDE to make and debug the shared library.
I don’t have idea. Help!
It is normal!
thanx All !
Use the -l c option
code:
typedef int (*foofunc)( int );
funcptr temp;
/*Gain access to an executable object file */
/*shared library path → “/root/shared_lib/libsharecreat.so” */
handle = dlopen("/root/shared_lib/libsharecreat.so" ,RTLD_NOW );
if(handle == NULL)
{
printf(“link library open failed\n”);
exit(0);
}
/Get the address of a symbol in a shared object-function 1/
/* “TestFM” is function 1’s name*/
brain = (foofunc) dlsym( handle, “TestFM” );
if(brain == NULL)
{
printf(“link library open failed\n”);
exit(0);
}
printf(“temp is=%d\n”,(*brain)(10));