共享库和共享变量 [英] Shared libraries and shared variables

查看:83
本文介绍了共享库和共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我有2个共享库(让它们分别为1.so,2.so)和程序(a.out).
2.so链接到1.so和a.out-它具有在1和a中使用的某些功能.
2.so的代码是

Hello everyone !
I have 2 shared libraries(let them be 1.so,2.so) and program(a.out).
2.so is linked to 1.so and a.out - it has some functions that is used in 1 and a.
The code of 2.so is

FILE *in;
char filename[128];
int func_printer(int a)
{
if(strlen(filename)==0)
{
   sprintf(filename,"%ld",time(NULL);
}
if((in=fopen(filename,"a"))==NULL)return;
fprintf(in,"%i",a);
fclose(in);
}


a.out有next


a.out has next

extern int func_printer(int);
extern void some_action();
int main()
{
some_action();
func_printer(2);
return 0;
}


最后1.so有方法some_action


And finally 1.so has method some_action

extern int func_printer(int);
void some_action()
{
func_printer(1);
printf("hello world");
return;
}


因此,当a.out启动时,它将调用1.so(some_action())并调用2.so(func_printer).它创建名称为timestamp(t1)的文件,并在其中写入一些信息.之后,1.so调用2.so(func_printer)并创建另一个带有时间戳的文件.

在这种情况下,some_action总是可以写入t1,但是当程序再次启动时,它应该写入另一个文件.总而言之,简单的说,就是程序启动时,所有库都应该知道文件名写在哪里(而不用像char * filename ="somefile.txt"那样硬预定义文件名)?


So when a.out starts, it call''s 1.so(some_action()) and it call''s 2.so(func_printer). It create file with name of timestamp(t1), write in it some info. After that 1.so calls 2.so(func_printer) and it creates another file''s with timestamp.

Is it possible in this situation that some_action write always to t1, but when program starts again it should write to another file. All in all simply when program starts all libraries should know the filename where to write(without hard predifining the file name like char *filename="somefile.txt";)?

推荐答案

我认为您的代码不会达到您的期望,因为每次调用func_printer()时,字符串filename都会在堆栈中重新分配,并且第一次使用时不一定是空字符串.您应该在从主例程调用文件名时传入文件名(最佳选择),或者将其设置为静态对象以便正确初始化.
I do not think your code will do what you expect because the string filename will be reallocated on the stack every time you make a call to func_printer(), and will not necessarily be a null string the first time through. You should either pass the filename in when you call it from your main routine (best option), or make it a static object so it gets initialised correctly.


请参阅我对解决方案的评论1:最好在全局范围内使用任何内容,即使在只有一个可执行模块的应用程序中也是如此.通常,有一种方法可以避免这种情况.

您没有实现它的最终目标,所以我不确定您是否确实需要某些全局行为,您可能只是需要它.在这种情况下,您可以使用称为 Singleton 设计模式:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 [ ^ ],
http://en.wikipedia.org/wiki/Singleton_pattern [
Please see my comment to the Solution 1: it''s now a good idea to use anything global, even in an application having only one executable module. Usually, there is a way to avoid it.

You did not share the ultimate goal of it, so I cannot be sure that you really need some global behavior or not, you just might need it. In this case, you can use the design pattern called Singleton:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Singleton_pattern[^].

For some code samples and discussion on pro and contra, please also see the reference in this article.

—SA


这篇关于共享库和共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆