[CMake] shared libraries on windows

Yuri Timenkov ytimenkov at parallels.com
Tue Jul 29 06:36:45 EDT 2008


On Tuesday 29 July 2008 14:27:44 christian wrote:
> Hello:
>
> I am starting with cmake in order to have my project running on both
> linux and windows.
>
> In my project I create a library and several console tools linked with
> this library.
>
> I would like to use shared libraries (.so in linux and .dll in windows)
>
> In my CMakeLists.txt I have for the library target domething like:
>
> ADD_LIBRARY(MYLIB SHARED ${MYLIB_SRC})
>
> The tools are linked like:
>
> TARGET_LINK_LIBRARIES(Tool1 MYLIB)
>
> In Linux and Cygwin this works fine. However in Windows cmake creates
> the dll file but tries to link with the .lib file, which does not exist.
>
> Can anybody help me?
You didn't export symbols from your library.
Write header like this:

#ifdef _WIN32
#	ifdef MYLIB_EXPORTS
#		define MYLIB_API __declspec(dllexport)
#	else
#		define MYLIB_API __declspec(dllimport)
#	endif
#else
#	define MYLIB_API
#endif // WIN32
(Or look into classes which Visual Studio generates for you).

And prefix all your classes and functions:
class MYLIB_API MyClass
{};
int MYLIB_API myFunction();

CMake will automatically define symbol MYLIB_EXPORTS when compiling your 
library.

Alternatively you can use .def files for linker.

>
>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list