LNK2001 & LNK1120 when including DLL in project - MSVC++

882 views Asked by At

I have a DLL and lib file. I've included them in the root source directory and added the lib reference through Additional Dependencies. However, I get the following error:

1>main.obj : error LNK2001: unresolved external symbol "class game::c_State game::state" (?state@game@@3Vc_State@1@A)
fatal error LNK1120: 1 unresolved externals

which would be referencing this from "engine.h":

extern __declspec(dllexport) c_State state;

In "state.cpp" (from the DLL's source,) it is declared as

namespace game
{
    c_State state;
    //clipped for relevance
}

Could it be that I need to place the DLL somewhere specific? Does Windows know where to look? I found nowhere in the properties to specifically reference the DLL file, only the lib file.

Also, do I need a __declspec(dllexport) when declaring variables, or only functions?

Thanks in advance!

1

There are 1 answers

1
Hans Passant On

You have to apply __declspec(dllexport) to the definition, not the declaration. Also, the declaration needs __declspec(dllimport) in the other project. So in the .h file:

#undef EXPORT
#ifdef FOO_EXPORTS
#  define EXPORT __declspec(dllexport)
#else
#  define EXPORT __declspec(dllimport)
#endif

extern EXPORT int shared;

In the DLL source code file:

__declspec(dllexport) int shared;

And in the DLL project use Project + Properties, C/C++, Proprocessor. Add FOO_EXPORTS to the preprocessor definitions.