File zts_usetlib.cpp: source of program who calls the library function foo():
1 2 3 4 5 6 7
Well, when I access the global variable logname the program ends (dbg.txt contains only "foo").
The original program is a dll which is called by a VB COM wrapper, which is instantiated by PHP when a web page is loaded: when I read or write the library global variable Apache crashes.
There's something I'm missing...
Thanks in advance!
The library does not export the global variable, so you are not supposed to have access to it. (In other words, it is not really a 'global' variable, since only the library properly knows about it.)
Remember, stuff in .h files is for the users of the libary. Everything else belongs to the library only.
If you wrote the library yourself, you can explicitly export it in the .h file: extern string logname;
After that you can use it in your main() or anywhere else...
Beware that you must compile the library and your program with the same compiler and STL implementation. If you, for example, compile your library with MSVC++ and then compile your program with Watcom C++, chances are that accessing that string variable will cause problems.
I don't export the global variable logname because I need to use it in many functions of my library but I don't need it in the main program. Or do I need to export it anyway because it's a global?
The actual purpose is this: I have a string logname and a ofstream zlog, which I use for debugging; the exported function "foo" calls other internal functions which call other functions, and so on.
I would like to be able to do zlog << something in every function and I would open the stream at the beginning of foo() and close it at the end of foo(), hence the two variables should be 'global' for the library functions only. Is that possible?
The library is mine and I compiled library and program with the free Borland 5.5.
No, the file is one, and it's opened each time the library is called and closed before returning to the main program.
In the example I made logname as global and the ofstream as local, just because I found that accessing a global variable the program ends, but they're both global.
My problem is that the ofstream must be visible to all functions inside the library and must be managed entirely by the library itself. I don't want to pass the ofstream as an argument to every internal (not exported) function.
The first code didn't work because you tried to access an invalid object (that is, one you aren't supposed to be able to see).
I don't understand this: why shouldn't I be able to see logname if it's a global variable?
There is scope and visibility. The two aren't necessarily synonymous.
Scope is how accessible an object is according to syntax. The variable is technically a global variable (well, it probably is, depending on your compiler), hence it has global scope.
Visibility is where an object can be seen. Though the variable is global, it is not visible outside of the .cpp file, because it is only declared within that file's compilation. Compiling another file is another file -- there is no reason why that other file should have any idea what is in this file.
Hence, if you want the variable to be global, you must explicitly list it as extern in the .h file.
1 2 3 4 5 6 7 8 9 10 11 12
// This is the type and name of a variable defined elsewhere (in peekaboo.cpp)
extern std::string I_am_visible;
// This is the type and name of a function defined elsewhere (in peekaboo.cpp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Here is the definition of that string
string I_am_visible = "Hello world!";
// Here is a string that, while in global scope, is local to this .cpp file only
string I_am_invisible = "WoooOOOOooo!";
// Here is that function
cout << I_am_invisible << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13
// Include the file that tells us all about the things we can see/use from peekaboo.cpp
// Use the things we can see
cout << I_am_visible << endl;
However, I fail to see what's wrong in my code: the declaration string logname is in the library .cpp and is used in the library .cpp, so its scope and its visibility should be the entire .cpp.
If I move it to the .h making it external I get the same behaviour: the program ends when I access it in the foo() function.
Maybe I'm missing what's the scope of a variable declared outside functions in a .cpp of a library.
And I've just realized I was not very clear: I first make the static library compiling the zts_tlib.h and zts_tlib.cpp, then I make the .exe program compiling the zts_usetlib.cpp and linking it to the previously generated .lib.