如何寫shared library 或是static library

中文QNX 讨论: 欢迎大家灌水,讨论有关QNX 问题。
回复
robby0524
Senior Member
帖子: 38
注册时间: 周一 6月 19, 2006 4:14 pm

如何寫shared library 或是static library

帖子 robby0524 » 周三 3月 14, 2007 4:39 pm

請問如何在QNX IDE中作shared Library或是static library
以下我的code,說我未定義,不知如何debug

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'

shared library 和static library的最大差別是如何,可以解說一下嗎?

robby0524
Senior Member
帖子: 38
注册时间: 周一 6月 19, 2006 4:14 pm

帖子 robby0524 » 周五 3月 16, 2007 8:45 am

$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!
難道IDE沒有這個功能嗎?

xtang
Moderator
帖子: 1816
注册时间: 周五 9月 12, 2003 4:14 am
来自: China

帖子 xtang » 周五 3月 16, 2007 11:52 am

你要开两个project。

shared.c一个,定义它为 shared library.
hello.c 另一个,定义它为 application.

在hello这个project的properties里,找到 "linker",而后把category设成extra libraries。在下面追加 shared。这样每次Build Hello这个project时,它就会自动链接shared了。

另外,在定义Hello的执行(Run As...)时,也要在"download"里面追加 shared这个Project,这样在执行Hello时,IDE会同时自动加载libshared.so

robby0524
Senior Member
帖子: 38
注册时间: 周一 6月 19, 2006 4:14 pm

帖子 robby0524 » 周五 3月 16, 2007 3:06 pm

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));

回复