[Cmake] LNK 2001 error

Brad King brad.king at kitware.com
Thu, 19 Feb 2004 11:37:30 -0500


Adolfo González Uzábal wrote:
> Hello everybody there.
> 
> I'm trying to compile an application that uses a .dll I've just compiled with 
> Cmake.
> 
> I'm using __declspec(dllexport) and __declspec(dllimport) in my functions 
> depending on the CMake generated "my_Library_name"_EXPORTS variable.
> 
> I can compile the .dll but when I try to use it, I got this kind of error:
> 
> [...]error LNK2001: unresolved external symbol "__declspec(dllimport) [...]
> 
> I know from the forums that this is a common error but I've not found the way 
> for solving it.
> 
> What have I missed?

You need to check two things:

1.) Make sure the library is actually built as a shared library and not 
a static library.  If you only support building it as a shared library, 
then you can use a command like this:

ADD_LIBRARY(mylib SHARED src1.cxx src2.cxx ...)

The SHARED keyword tells CMake to always build the library shared.

2.) Make sure your executable is linking to the library.  Use a command 
like this:

TARGET_LINK_LIBRARIES(myexe mylib)

-Brad