Click here to Skip to main content
11,806,026 members (60,562 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C Linux
Hello everyone !
I have 2 shared libraries(let them be, and program(a.out). is linked to and a.out - it has some functions that is used in 1 and a.
The code of is
FILE *in;
char filename[128];
int func_printer(int a)
a.out has next
extern int func_printer(int);
extern void some_action();
int main()
return 0;
And finally has method some_action
extern int func_printer(int);
void some_action()
printf("hello world");
So when a.out starts, it call's and it call's It create file with name of timestamp(t1), write in it some info. After that calls 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 4:11am
Rate this: bad
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.
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.
Sergey Alexandrovich Kryukov at 5-Oct-12 21:05pm
Good catch, a 5. The problem itself is easy enough...
Rate this: bad
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:[^],[^].

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

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 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!
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?
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.
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.

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

  Print Answers RSS
0 Richard Deeming 225
1 OriginalGriff 215
2 Sergey Alexandrovich Kryukov 170
3 Abhinav S 120
4 ppolymorphe 115
0 OriginalGriff 3,330
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,905
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2015
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