HI,

I have an application that look like

app.exe depends static lib 1 and static lib 2
static lib 2 depends on static lib 1

in static lib 1, I have one header file and one cpp file
header:
extern const int kNotUsed;
cpp:
const int kNotUsed = -1;

The weird thing is that I can use kNotUsed in my app.exe. However, I
can NOT use kNotUsed from static lib 2. Also, I used

dumpbin /symbols StaticLib1.lib

and I could NOT find kNotUsed.

Thus, my questions are
1. why can I use kNotUsed from app but not from static lib2?
2. if kNotUsed is not in the symbols, how can app.exe use it?

I tried to reproduce the problem in a dummy project and I could NOT
reproduce it. not sure what is wrong. totally lost.

Thank you for any advices,

Re: symbols in static libraries by Alex

Alex
Sat Jul 19 02:45:42 CDT 2008

"Coward 9" wrote:
> HI,
>
> I have an application that look like
>
> app.exe depends static lib 1 and static lib 2
> static lib 2 depends on static lib 1
>
> in static lib 1, I have one header file and one cpp file
> header:
> extern const int kNotUsed;
> cpp:
> const int kNotUsed = -1;
>
> The weird thing is that I can use kNotUsed in my app.exe.
> However, I can NOT use kNotUsed from static lib 2. Also, I used
>
> dumpbin /symbols StaticLib1.lib
>
> and I could NOT find kNotUsed.
>
> Thus, my questions are
> 1. why can I use kNotUsed from app but not from static lib2?

Something went wrong with your configuration. I tried this setup
and everything works just fine.

Ensure that you include "StaticLib1.h" everywhere you intend to
use `kNotUsed' variable. Don't forget to include it in
"StaticLib1.cpp", too.

HTH
Alex



Re: symbols in static libraries by Norbert

Norbert
Sat Jul 19 07:25:22 CDT 2008


Coward 9 schrieb:

> I have an application that look like
>
> app.exe depends static lib 1 and static lib 2
> static lib 2 depends on static lib 1
>
> in static lib 1, I have one header file and one cpp file
> header:
> extern const int kNotUsed;
> cpp:
> const int kNotUsed = -1;
>
> The weird thing is that I can use kNotUsed in my app.exe. However, I
> can NOT use kNotUsed from static lib 2. Also, I used
>
> dumpbin /symbols StaticLib1.lib
>
> and I could NOT find kNotUsed.
>
> Thus, my questions are
> 1. why can I use kNotUsed from app but not from static lib2?
> 2. if kNotUsed is not in the symbols, how can app.exe use it?

Maybe this a C/C++ name mangling problem?
Are all your three projects using the same language, or are you mixing C and C++
code? If you need to access kNotUsed from both C and C++ code you must declare
it as extern "C" for C++ code.

Norbert

Re: symbols in static libraries by Coward

Coward
Sat Jul 19 08:27:42 CDT 2008

HI,

Thank you for your reply

Alex: I tried that setup myself in a separate solution/project
myself. it worked too. StaticLib1.h is included everywhere I use
kNotUsed. Otherwise, compile will NOT succeed, right? Also
StaticLib1.cpp is included in static lib 1 project(otherwise, I will
not be able to reference it from app.exe)

Norbert: I am not mixing C/C++ here. All projects are being developed
in VS2005.

As a bonus of sleep overnight, I found the solution. After I include
StaticLib1.h in StaticLib1.cpp(at the begining, I did not), the link
error IS GONE. Also,

0BF 00000000 SECT30 notype External | ?kNotUsed@@3HB (int
const kNotUsed) is showing up in the output of

dumpbin /symbols StaticLib1.lib

Do you know what configurations regarding whether to export a symbol
or not from a static library I am missing here?

Thank you again for your helps,



On Jul 19, 8:25=A0am, Norbert Unterberg <nunterb...@newsgroups.nospam>
wrote:
> Coward 9 schrieb:
>
>
>
>
>
> > I have an application that look like
>
> > =A0app.exe =A0 depends static lib 1 and static lib 2
> > =A0static lib 2 depends on static lib 1
>
> > in static lib 1, I have one header file and one cpp file
> > header:
> > =A0 =A0 =A0 =A0 =A0extern =A0 =A0const int =A0 =A0 =A0 kNotUsed;
> > cpp:
> > =A0 =A0 =A0 =A0 =A0const int kNotUsed =3D -1;
>
> > The weird thing is that I can use kNotUsed in my app.exe. =A0However, I
> > can NOT use kNotUsed from static lib 2. =A0Also, I used
>
> > =A0 =A0 =A0 =A0 =A0dumpbin /symbols StaticLib1.lib
>
> > and I could NOT find kNotUsed.
>
> > Thus, my questions are
> > 1. =A0why can I use kNotUsed from app but not from static lib2?
> > 2. if kNotUsed is not in the symbols, how can app.exe use it?
>
> Maybe this a C/C++ name mangling problem?
> Are all your three projects using the same language, or are you mixing C =
and C++
> code? If you need to access kNotUsed from both C and C++ code you must de=
clare
> it as extern "C" for C++ code.
>
> Norbert- Hide quoted text -
>
> - Show quoted text -


Re: symbols in static libraries by Alex

Alex
Sat Jul 19 09:03:44 CDT 2008

"Coward 9" wrote:
> Do you know what configurations regarding whether to export a
> symbol or not from a static library I am missing here?


You did not miss anything. Apparently, your error was not to
inlcude "StaticLib1.h" in "StaticLib1.cpp". Instead of including
"StaticLib1.h" in "StaticLib1.cpp" you could have written in a
.CPP file:

extern const int kNotUsed = -1;

The "extern" specifier is important here, since it tells the
compiler that the `kNotUsed' variable is used elsewhere.

Alex





Re: symbols in static libraries by Norbert

Norbert
Sat Jul 19 17:21:40 CDT 2008



Coward 9 schrieb:
> HI,
>
> Thank you for your reply
>
> Alex: I tried that setup myself in a separate solution/project
> myself. it worked too. StaticLib1.h is included everywhere I use
> kNotUsed. Otherwise, compile will NOT succeed, right? Also
> StaticLib1.cpp is included in static lib 1 project(otherwise, I will
> not be able to reference it from app.exe)
>
> Norbert: I am not mixing C/C++ here. All projects are being developed
> in VS2005.

Just for the record, VS2005 includes both a C and C++ compiler, so it is
possible and very easy to mix C and C++ in the same workspace, even in the same
project.

> As a bonus of sleep overnight, I found the solution. After I include
> StaticLib1.h in StaticLib1.cpp(at the begining, I did not), the link
> error IS GONE.

Great news. But I must admit that I still do not understand what caused the
error in the first place, and why including the header resolved the porblem.

Norbert


Re: symbols in static libraries by Alex

Alex
Sun Jul 20 01:27:48 CDT 2008

"Norbert Unterberg" wrote:
> But I must admit that I still do not understand what caused the
> error in the first place, and why including the header resolved
> the porblem.

The problem was that implementation file, namely StaticLib1.cpp,
didn't specify the variable as "extern". So, all other parts of
the program could see the declaration of it, so compilation
padssed successfully, but the linker failed to find the
definition. After the inclusion of StaticLib1.h (where the
variable declared as "extern") into StaticLib1.cpp the compiler
matched the definition with the declaration and placed it in
StaticLib1.obj.

Alex



Re: symbols in static libraries by Coward

Coward
Sun Jul 20 10:13:15 CDT 2008

TBH, I still don't understand either. Without including staticlib1.h
in staticlib1.cpp, why can my app.exe acess kNotUsed? (KNotUsed does
not exist in symbol table).

If you guys are interested,let me know, I can share my desktop with
you and let you check the problem.




On Jul 20, 2:27=A0am, "Alex Blekhman" <tkfx.REM...@yahoo.com> wrote:
> "Norbert Unterberg" wrote:
> > But I must admit that I still do not understand what caused the
> > error in the first place, and why including the header resolved
> > the porblem.
>
> The problem was that implementation file, namely StaticLib1.cpp,
> didn't specify the variable as "extern". So, all other parts of
> the program could see the declaration of it, so compilation
> padssed successfully, but the linker failed to find the
> definition. After the inclusion of StaticLib1.h (where the
> variable declared as "extern") into StaticLib1.cpp the compiler
> matched the definition with the declaration and placed it in
> StaticLib1.obj.
>
> Alex


Re: symbols in static libraries by Alex

Alex
Sun Jul 20 10:20:03 CDT 2008

"Coward 9" wrote:
> TBH, I still don't understand either. Without including
> staticlib1.h in staticlib1.cpp, why can my app.exe acess
> kNotUsed? (KNotUsed does not exist in symbol table).

I don't know why, because in my project it consistently fails to
link.

Alex



Re: symbols in static libraries by Doug

Doug
Sun Jul 20 10:30:23 CDT 2008

On Sun, 20 Jul 2008 08:13:15 -0700 (PDT), Coward 9 <Coward9@gmail.com>
wrote:

>TBH, I still don't understand either. Without including staticlib1.h
>in staticlib1.cpp, why can my app.exe acess kNotUsed? (KNotUsed does
>not exist in symbol table).

What do you mean by "access"? Some contexts don't evaluate variables, such
as sizeof(kNotUsed). If you are using it in such a way that requires it to
exist, try using the /verbose linker option; if it's coming from a library,
that will tell you where it's coming from. Otherwise, you can try compiling
the files in app.exe that use kNotUsed with /FAs and examining the assembly
code.

--
Doug Harrison
Visual C++ MVP

Re: symbols in static libraries by Igor

Igor
Sun Jul 20 11:03:40 CDT 2008

"Coward 9" <Coward9@gmail.com> wrote in message
news:abcb3846-f68a-4967-b2fb-c7ceb70393b8@2g2000hsn.googlegroups.com
> in static lib 1, I have one header file and one cpp file
> header:
> extern const int kNotUsed;
> cpp:
> const int kNotUsed = -1;
>
> The weird thing is that I can use kNotUsed in my app.exe. However, I
> can NOT use kNotUsed from static lib 2.

At namespace scope, 'const' implies 'static' unless overridden by
'extern':

3.5/3 A name having namespace scope (3.3.5) has internal linkage if it
is the name of
- an object or reference that is explicitly declared const and neither
explicitly declared extern nor previously declared to have external
linkage

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925