[CMake] Creating libraries (DLL) in windows

Mike Jackson mike.jackson at imts.us
Fri Aug 1 18:26:01 EDT 2008


Well, basically, Cmake will _help_ you create your library BUT there  
is some work that you need to add to your project.

Say your library is named "mylib" and Your project name is MyLib.

When you do
Add_library(MyLib SHARED ${Sources}) cmake will automatically add a  
definition "MyLib_EXPORT" to the compiler invocation on windows.
In my example you will also need to define SET (BUILD_SHARED_LIBS  
TRUE) in your cmakelists.txt file.

Now to use that bit of information you need to have something like  
the following in a header file that gets included by all your classes:

#if defined (WIN32) && defined (BUILD_SHARED_LIBS)
#if defined (_MSC_VER)
#pragma warning(disable: 4251)
#endif
   #if defined(MyLib_EXPORT)
     #define  MYLIB_EXPORT __declspec(dllexport)
   #else
     #define  MYLIB_EXPORT __declspec(dllimport)
   #endif
#else
  #define MYLIB_EXPORT
#endif


Now, in the class declaration for each of your classes add the  
definition:

class MYLIB_EXPORT MyClass
{

};

So what happens when all of this is implemented is that on non  
windows platforms the MYLIB_EXPORT is defined to NOTHING and has not  
affect. On Windows MYLIB_EXPORT will get defined to __declspec 
(dllexport) when you actually compile the library or __declspec 
(dllimport) when the library is being linked to by another library or  
executable.

I hope all this helps.

Take a look at <http://titanium.imts.us/viewvc/Task_7/MXADataModel/>  
then src/Common/DLLExport.h for a working example.

-- 
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services


On Aug 1, 2008, at 6:10 PM, Leopold Palomo Avellaneda wrote:

> Hi,
>
> I'm a Linux user and I'm uncomfortable in the windows environment.  
> In Linux, I
> can create a C++ library with cmake without any problem. The so  
> version and
> the .a (static version).
>
> But, my question is about windows. If I have a shared library that  
> works in
> Linux, may I have to do something more to have a dll in windows?  
> Because, I
> have read about the  __declspec(dllexport)  and __declspec 
> (dllimport) and I'm
> a bit lost.
>
> Thanks in advance,
>
> Leo
>
>
> -- 
> --
> Linux User 152692
> PGP: 0xF944807E
> Catalonia
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake




More information about the CMake mailing list