Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Linux
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 5-Oct-12 5:11am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
steph Zagainov at 5-Oct-12 11:11am
   
The question is how to make global name to all libraries and program
Richard MacCutchan at 5-Oct-12 12:56pm
   
What do you mean, they are already global?
Sergey Alexandrovich Kryukov at 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 at 5-Oct-12 21:05pm
   
Good catch, a 5. The problem itself is easy enough...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
steph Zagainov at 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 at 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 at 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 at 8-Oct-12 2:53am
   
???
steph Zagainov at 8-Oct-12 3:11am
   
I shouldn't use here any oop.
Sergey Alexandrovich Kryukov at 8-Oct-12 13:29pm
   
You did not get that OOP was for the analogy, did you? Your "I shouldn't" is your own mysterious believe; please deal with it by yourself. You are free to use whatever you want, of course; but here there is a different situation: you are asking for the advice, and the advice is: global is bad. If you don't want to listen for the advice, it's your problem, but you totally irrational arguments are not so interesting here. If you follow the advice, you won't have any problem; and it does not have to be "formal" OOP, so you could use C. If this is C++, use OOP.
 
It does not change the situation with globals. You can export only functions and never variables, because a function can return value or pointer, to be used in a different executable module. Are you getting the idea?
--SA
steph Zagainov at 8-Oct-12 16:51pm
   
Can you simply read question? Do you know how shared libraries work?
Sergey Alexandrovich Kryukov at 8-Oct-12 18:02pm
   
Yes I do, so what? I tell you, don't use exported variable, to solve the problem.
--SA
steph Zagainov at 8-Oct-12 23:43pm
   
"Don't use" is not an option. Do you really read question?(Because it's not about what use or not)
Sergey Alexandrovich Kryukov at 8-Oct-12 23:56pm
   
Enough is enough. Thank you for your interest and this discussion.
--SA

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

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 175
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100