views:

29

answers:

1

In Visual Studio 2008, I have a solution which contains two projects: one project is a .dll, the other is a command line application which calls the .dll.

If I remove one of the files ("ast.c"), then add it straight back in, I get the following error.

How do I fix this error?

Error 22 error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ast_get_int_value(struct ast_node *,void *)" (_imp?ast_get_int_value@@YAHPAUast_node@@PAX@Z) referenced in function "double __cdecl d_low(struct ast_node *,struct ast_node *,struct ast_node *,struct ast_node *,void *)" (?d_low@@YANPAUast_node@@000PAX@Z) main.obj main

+1  A: 

Got it - had to right click on the project, go into "Linker" and "Input" and "Additional Dependencies", and add "..\$(ConfigurationName)\dll.lib". The file "dll.lib" is created as part of the compile process for the .dll, and we need to tell the linker where this file is so it can resolve any external dependencies. This fixed the linking errors once and for all.

Some nice tips from http://www.codeproject.com/KB/DLL/XDllPt1.aspx that explain the linking process:

  • The DLL exports its symbols, and the application imports the DLL symbols. When compiling the application, the compiler sees the DLL symbols via an include file (Test.h). When linking the application, the linker sees the DLL symbols via the import library (Test.lib).
  • The DLL must be in the same directory as the EXE when running the application. In early versions of Windows, it was acceptable practice to put application DLLs in the Windows or System directories, but this is now recognized to cause problems, and should not be done.

And also see:

http://support.microsoft.com/kb/815065

And:

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/a22571ad-ef89-4f94-afca-4f4d32cdd0f2

Gravitas