error LNK2019: unresolved external symbol -- Why?
I am trying simple static linking with VS Express 2005 (C++). I have headers and source for a very simple sample static library and source of a very simple sample executable using that library.
-
Static Library Header Sample:
-
//returner.h
#ifndef _RET_
#define _RET_
#ifdef _LIB
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC __declspec(dllimport)
#endif
int DECLSPEC returner();
#endif
-
Static Library Source Sample:
-
//returner.cpp
#include "..\\returner\\returner.h"
int DECLSPEC returner()
{
return 1;
}
The above compiles well No Problem.
--
Executable Source Sample:
--
exec.cpp
#include "..\\returner\\returner.h"
int main(int argc, char** argv)
{
return returner();
}
This does not link telling me that it can't resolve the external symbol returner. The library returner is added to the additional dependencies of the executable project. I would like to link statically the returner lib to the executable using the two different solutions but unlike my usual style with Visual Studio, I cannot with Express 2005.I have checked for C/C++ issues just in case (extern "C") but as expected, is not the case.
Any hints? Anything new in VS 2005 Expre?
Thanks
If your trying to compile a static library, the __declspec(dllexport) is not needed. That's only if the library is compiled into a .dll and loaded dynamically at runtime (I think. Can someone verify this?).If your using a static library, the linker needs a reference to "returner". I'm not sure of the procedure in VS2005 Express, but try this:
1. Go to "Project->Properties->Link"
2. Find the "Additional Library Directories" field and add the path the the returner library
3. Find the "Additional Librarys" field and add the name of the library (for example: Returner.lib )
4. Rebuild
I'm not sure of VS2005 Express (as I haven't used it), but I know this is how you add a static lib at a project in .NET 2003/2005.
You don't use declspec(dllimport) or declspec(dllimport) for a static lib, you only use them for importing and exporting from a dll.
After removing the delcspecs, your example works perfectly for me (and I get the same error with them in).
Ronald Laeremans
Visual C++ team
Thanks all for your comments.
I did find it a bit strange in fact coz I have been compiling without the declspec unsuccessfully with static libs. I have been using declspec with dlls and I am suspecting that the declaration I had in my prog, was different than the implementation without me recalling so at the time of compilation.
I still have to try out the linking process again but I thank all for the very useful comments provided!
Thanks
Duncan