Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 has next
extern int func_printer(int);
extern void some_action();
int main()
{
some_action();
func_printer(2);
return 0;
}

And finally 1.so has method some_action
extern int func_printer(int);
void some_action()
{
func_printer(1);
printf("hello world");
return;
}

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";)?
Posted

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.
 
Share this answer
 
v2
Comments
steph Zagainov 5-Oct-12 11:11am    
The question is how to make global name to all libraries and program
Richard MacCutchan 5-Oct-12 12:56pm    
What do you mean, they are already global?
Sergey Alexandrovich Kryukov 5-Oct-12 21:07pm    
Using global name is not a good idea, to start with. If you tell use the ultimate goal of it, you could get some better advice. In worst case, you can implement singleton.
So, please see my answer.
--SA
Sergey Alexandrovich Kryukov 5-Oct-12 21:05pm    
Good catch, a 5. The problem itself is easy enough...
--SA
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
 
Share this answer
 
Comments
steph Zagainov 6-Oct-12 1:37am    
Singleton in global scope is "class" replacement of global objects, so there is no need of it. And will his work be different to global variable in terms of shred library( he created in it) and in program (where he also created).
Sergey Alexandrovich Kryukov 8-Oct-12 1:39am    
...so there is not need? Theoretically speaking, classes are replacement of something which could be written in machine codes, so what, no need, too? OK, let's code in machine language -- did you ever try? This comment shows total lack of judgement!
--SA
steph Zagainov 8-Oct-12 2:21am    
I just wanted to say that in "my question" there is no need for classes, just basic how it works.
Sergey Alexandrovich Kryukov 8-Oct-12 2:53am    
???
steph Zagainov 8-Oct-12 3:11am    
I shouldn't use here any oop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900