Answered by:
Static library & LNK4221 error

Question
-
Hi,
I'm getting this error from the linker:
foo.obj : warning LNK4221: no public symbols found; archive member will be inaccessible
My configuration:
Windows 7, 64 bit, VS2008 with all updates and SPs done.
My project:
A little win32 static library ( native code ) with about 20 cpp files plus .h files.
There is no precompiled header.
I'm using the convention where each couple of Foo.h and Foo.cpp file contains a single definition of the class Foo.
Almost all *.h and *.cpp files contain a single class with all inline methods. All classes have puclic methods as well.
The issue:
I've done a lot of static libraries in the past but no linker error of that type came out.
The strange thing is that the link error above comes out only for 3 or 4 classes over 20 classes the static library is composed of.
The link error above comes out when I compile the static library alone and there is no exe the library is linked with for now.
Any idea?
Thanks
Saturday, March 31, 2012 3:21 PM
Answers
-
According to official documentation for LNK4221, this is not an error that always prevents the creation of a valid static library. Probably foo.cpp only contains #include “foo.h” . If so, then try commenting this line until you need it.
I would also suggest you to reintroduce the usage of precompiled headers.
- Proposed as answer by Helen Zhao Tuesday, April 3, 2012 1:53 AM
- Marked as answer by Helen Zhao Monday, April 9, 2012 2:10 AM
Saturday, March 31, 2012 4:54 PM -
CodeVisio wrote:
I'm getting this error from the linker:
foo.obj : warning LNK4221: no public symbols found; archive member will be inaccessibleThis means you don't define any externally visible functions or global variables in foo.cpp. Thus, when you eventually link with this library, nothing from this file can actually be linked into the final executable.
Igor Tandetnik
- Proposed as answer by Helen Zhao Tuesday, April 3, 2012 1:53 AM
- Marked as answer by Helen Zhao Monday, April 9, 2012 2:10 AM
Saturday, March 31, 2012 6:54 PM
All replies
-
According to official documentation for LNK4221, this is not an error that always prevents the creation of a valid static library. Probably foo.cpp only contains #include “foo.h” . If so, then try commenting this line until you need it.
I would also suggest you to reintroduce the usage of precompiled headers.
- Proposed as answer by Helen Zhao Tuesday, April 3, 2012 1:53 AM
- Marked as answer by Helen Zhao Monday, April 9, 2012 2:10 AM
Saturday, March 31, 2012 4:54 PM -
The foo.h file includes many other classes and so other .h files. Foo class has many other classes (HasA relation) as members.
Apparently, other classes with same 'structure', don't suffer of the same problem.
Strangely enough, if I comment some part of a method inside the Foo class the linker warning will disappear. Obviously the commented code doesn't relate in any way with symbols or something like that.
Strangely enough, If I introduce a new class, let's say Foo19, in the static library then another class ( different from the one introduced ( let's say Foo5 ) ), will appear with the linker warning. Obiously before introducing Foo19, Foo5 didn't suffer of linker warning.
In reality what I was care of is the content of the linker warning message. If I understand correctly, no symbols from Foo class will be available from the static library and this is bad.
I'm not able to reproduce the problem from a fresh static library.
Any other idea?
Why do you suggest to use precompiled headers? for compilation speed reason?
Thanks
Saturday, March 31, 2012 6:52 PM -
CodeVisio wrote:
I'm getting this error from the linker:
foo.obj : warning LNK4221: no public symbols found; archive member will be inaccessibleThis means you don't define any externally visible functions or global variables in foo.cpp. Thus, when you eventually link with this library, nothing from this file can actually be linked into the final executable.
Igor Tandetnik
- Proposed as answer by Helen Zhao Tuesday, April 3, 2012 1:53 AM
- Marked as answer by Helen Zhao Monday, April 9, 2012 2:10 AM
Saturday, March 31, 2012 6:54 PM -
@Igor
what do you mean exaclty by "...you don't define any externally visible functions or global variables in foo.cpp" ?
Saturday, March 31, 2012 7:02 PM -
Saturday, March 31, 2012 7:24 PM
-
ok,
so I've right understood before.
In my first post I wrote:
1)
"Almost all *.h and *.cpp files contain a single class with all inline methods. All classes have puclic methods as well."
normal classes declaration plus public methods inside them makes them normal symbols exported from a static library.
Furthemore, I wrote:
2)
"The strange thing is that the link error above comes out only for 3 or 4 classes over 20 classes the static library is composed of."
This behaviour doens't fit into your explanation.
Furthemore, I wrote
3)
"I've done a lot of static libraries in the past but no linker error of that type came out."
this should give me some basic knowledge how symbols are exported from a dll and static libraries.
Please, read more carefully my first post.
Thanks.
Saturday, March 31, 2012 7:34 PM -
CodeVisio wrote:
In my first post I wrote:
1)
"/Almost all *.h and .cpp files contain a single class with all inline methods.*All classes have puclic methods as well./"I can't see your code, but the linker can. And it doesn't see any public symbols there. You can argue with me all you want - that's unlikely to convince the linker that it's mistaken. So I suggest you try and figure out why your source file doesn't export any symbols, if you think it should. If you'd like further help, post the code here, or upload it somewhere where I can see it.
Please, read more carefully my first post.
If the situation were as you describe it in your post, you wouldn't be getting this message. Since you do, the reality is somehow different from your description. You must be missing something.
Igor Tandetnik
Saturday, March 31, 2012 7:44 PM