Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

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.

Thanks for that. I'm assuming the ifdef and else statements are for portability. If I decided to compile the engine under Linux I could simply remove FOO_EXPORTS and then it would (assuming otherwise portable code) compile. – screennameless Feb 2, 2012 at 1:22

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.