Yeah, sorry for the not real code, but I can't seem to reduce the real code
to a small compilable problem. Let me try and rephrase.
In the file where the storage space for these globals is created, if I don't
also have the extern declarations, the linker will say my globals (the ones
that are not pointers) will say there are undefined references to these
variables. In other words, unless I have an extern declaration of these
variables (the ones that are not pointers), the linker will not export them
to the other modules. Yet, it exports the pointers with or without including
the extern declarations.
Is it a C++ requirement that I use an extern declaration if I want to export
these symbols? Are they limited to the module if I don't?
I guess maybe a better question might be, are variables declared at file
scope exported or not? Does the export require an extern declaration?
I read this in the C++ Faq Lite " (By the way, static data at file-scope is
now deprecated in C++: don't do that.) ". What does this mean? Is it saying
what I did was bad?
BidEuchreApp.cc (where storage space is defined for the globals)
------------------
#include "AppConstants.hh"
wxMutex *mutex;
wxCondition *cond;
const wxString APP_NAME(wxT(PACKAGE_NAME));
const wxString APP_VERSION(wxT(PACKAGE_VERSION));
const wxString APP_COPYRIGHT(wxT("Copyright (C) 2005 John David Ratliff"));
const wxString APP_URL(wxT("
http://games.technoplaza.net/"));
BidEuchreApp::BidEuchreApp() {
mutex = new wxMutex();
cond = new wxCondition(*mutex);
}
------------------
AppConstants.hh
-------------------
class wxCondition;
class wxMutex;
class wxString;
extern const wxString APP_NAME;
extern const wxString APP_VERSION;
extern const wxString APP_COPYRIGHT;
extern const wxString APP_URL;
extern wxCondition *cond;
extern wxMutex *mutex;
---------------------------------
EuchreFrame.cc
-----------------------
#include "AppConstants.hh"
void EuchreFrame::helpAbout(wxCommandEvent& WXUNUSED(event)) {
wxString message = APP_NAME + wxT(' ') + APP_VERSION + wxT('\n') +
APP_COPYRIGHT + wxT('\n') + APP_URL;
wxString title = wxT("About ") + APP_NAME + wxT("...");
wxMessageBox(message, title, wxOK | wxICON_INFORMATION, this);
}
----------------------
Here is the actual code. I will post the entire compilable module if you
think it will help, but I have 30 some files and it requires wxWidgets 2.6
to compile. You can assume it does use cond and mutex somewhere, but not in
EuchreFrame.cc or BidEuchreApp.cc.
If you remove the include of AppConstants.hh in the BidEuchreApp.cc file,
you will get the following linker errors.
ui/EuchreFrame.o(.text+0x1266):C:/msys/1.0/home/jdratlif/bideuchre/source/ui/EuchreFrame.cc:77:
undefined reference to `APP_VERSION'
ui/EuchreFrame.o(.text+0x12b8):C:/msys/1.0/home/jdratlif/bideuchre/source/ui/EuchreFrame.cc:77:
undefined reference to `APP_COPYRIGHT'
ui/EuchreFrame.o(.text+0x130d):C:/msys/1.0/home/jdratlif/bideuchre/source/ui/EuchreFrame.cc:77:
undefined reference to `APP_URL'
ui/EuchreFrame.o(.text+0x13b5):C:/msys/1.0/home/jdratlif/bideuchre/source/ui/EuchreFrame.cc:78:
undefined reference to `APP_NAME'
collect2: ld returned 1 exit status
The question is, why does the linker find the cond and mutex variables, but
not the APP_XXX variables?
Game.cc
----------------------
void Game::setPause(bool paused) {
this->paused = paused;
cond->Signal();
}
----------------------
Just one of the places cond is used, and ld has no problem with
BidEuchreApp.hh not including AppConstants.hh.
Thanks,
--John Ratliff