Re: static library problems
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 16 Dec 2005 21:08:48 -0500
Do not declare variables in header files. This will lead to a nice set of disasters. The
variable itself must be declared in a .cpp file.
Note that if you can declare it as static, you can get away with it
static const LPTSTR NAME = _T("foo");
(Note that you should always treat char as an obsolete data type except in very limited
and restricted situations, and program Unicode-aware)
but this creates an instance of the variable in every module that includes the header
file.
It is not clear why linking the library is such a bad idea. I'd be inclined to simply do
extern const LPTSTR NAME;
the have a file that consisted entirely of the line
const LPTSTR NAME = _T("foo");
(It is very common in code I write to have .cpp files that contain exactly one variable
declaration, or contain exactly one function. It is a common myth to think that a file
represents some important amount of intellectual complexity and therefore fewer files
means a better program)
Note that you still have to mention the library in the link, but only those components
that need to be loaded will be loaded, in this case, only the file that declares the
variables. No big deal.
joe
On Fri, 16 Dec 2005 18:51:50 +0800, "Jerry Dong" <shichongdong80@xxxxxxxxx> wrote:
>Hi,
>I have a module which is a static library, in one of headers, i define a const string,like:
>const char* NAME = "foo";
>If i want to use this string in other modules, i just need include the header, i need not link this static lib.
>If i define some functions in static lib module, i need link the lib and include the header in other modules.
>Why in the first case i need not link the lib? Please correct me if i make some errors.
>
>Thanks
>-Jerry
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- static library problems
- From: Jerry Dong
- static library problems
- Prev by Date: Re: Begin of a Application...
- Next by Date: Re: How to disable AutoPlay programmatically
- Previous by thread: Re: static library problems
- Next by thread: Property***/Pages into Dialog
- Index(es):
Loading