[CMake] Win32: Link failure with ADD_LIBRARY SHARED

David Cole david.cole at kitware.com
Thu Nov 15 10:14:13 EST 2007


By default, nothing is exported from a DLL on Windows. You have to
decorate the code with __declspec(dllexport) or provide a .def file to
specify what gets exported.

Once you've exported something from the library, building it again
will produce a .lib file that you'll be able to link against.

Poke around the mailing list archives for export and DLL, or perhaps
take a look at the VTK source tree, where there are macros defined in
the headers to export classes conditionally.


HTH,
David


On 11/15/07, Stephen Collyer <scollyer at netspinner.co.uk> wrote:
> I have a trivial project that builds a single executable and
> a library against which it links. The source tree looks like:
>
> src/exe/test_prog.cpp
> src/lib/Test.cpp, Test.h
>
> When I set up the cmake files to build a static library the
> test_prog exe builds and runs fine on both Linux and Win32.
>
> When I add a SHARED specifier to the ADD_LIBRARY command for
> the Test library, the exe builds and run fine on Linux, but
> fails at link time when the projects are built under Visual
> Studio, with a message:
>
> LINK : fatal error LNK1104: cannot open file 'Test.lib'
>
> Now, I'm no Windows expert but AFAIK, linking against a
> DLL is done by looking at the contents of a .lib file,
> which indicates the symbols exported by the DLL. However,
> there's no .lib file at all in the build tree, so I'm
> confused as to what's going on here. Can someone explain ?
>
> In addition, now I've started thinking about it, I'm
> confused as to how cmake know whether to link statically
> or against shared libraries. Is this purely down to the
> presence or absence or SHARED in the ADD_LIBRARY commands ?
> i.e if a library is built shared, cmake tries to link against
> that, and if built static, similarly, and there's no way
> to override it.
>
> --
> Regards
>
> Steve Collyer
> Netspinner Ltd
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>


More information about the CMake mailing list