|
<b> CMake Version 2.8.6 Now has all of this functionality built in</b>
|
|
## CMake version 2.8.6 or later
|
|
|
|
|
|
|
|
CMake now has all of this functionality built in.
|
|
|
|
|
|
You need to add the following to your CMake file:
|
|
You need to add the following to your CMake file:
|
|
|
|
|
|
include (GenerateExportHeader)
|
|
include (GenerateExportHeader)
|
|
|
|
|
|
Then after you add your library using add_library(...) you should also
|
|
Then after you add your library using `add_library(...)` you should also
|
|
do the following in your CMake file.
|
|
do the following in your CMake file.
|
|
|
|
|
|
add_library(MyLibrary ${LIB_TYPE} ${srcs})
|
|
add_library(MyLibrary ${LIB_TYPE} ${srcs})
|
... | @@ -15,14 +17,15 @@ do the following in your CMake file. |
... | @@ -15,14 +17,15 @@ do the following in your CMake file. |
|
STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
|
|
STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
|
|
)
|
|
)
|
|
|
|
|
|
See
|
|
See the [`GenerateExportHeader` module documentation](https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html)
|
|
<http://cmake.org/cmake/help/cmake-2-8-docs.html#module:GenerateExportHeader>
|
|
for complete details.
|
|
for the complete help contents.
|
|
|
|
|
|
## CMake 2.8.5 and earlier
|
|
|
|
|
|
<b>PRE CMake 2.8.5 and earlier </b>
|
|
_(Note: The remainder of this text is retained for historical interest, however it is extremely out of date and possibly incorrect. It is highly unlikely that anyone working with CMake today is using a version before 2.8.6 (or any pre-3.0 version), so it would be best to consult more current information.)_
|
|
|
|
|
|
**How to build a Windows DLL (for the NON-Windows Developer). A Short
|
|
### How to build a Windows DLL (for the NON-Windows developer); a short
|
|
introduction.**
|
|
introduction
|
|
|
|
|
|
Those folks coming from an environment like Linux, OS X or even MinGW
|
|
Those folks coming from an environment like Linux, OS X or even MinGW
|
|
may have some initial issues when trying to create a DLL on windows.
|
|
may have some initial issues when trying to create a DLL on windows.
|
... | @@ -30,36 +33,34 @@ Usually the problems involve the library symbols NOT getting exported. |
... | @@ -30,36 +33,34 @@ Usually the problems involve the library symbols NOT getting exported. |
|
This article will try to walk you through what needs to be done in order
|
|
This article will try to walk you through what needs to be done in order
|
|
for you to bring your code to an MS compiler successfully.
|
|
for you to bring your code to an MS compiler successfully.
|
|
|
|
|
|
<b> This short explanation was taken from a recent post to the cmake
|
|
This short explanation was taken from a post to the cmake
|
|
mailing list and generally sums up the problem.</b>
|
|
mailing list, and generally sums up the problem:
|
|
<code> <i>Visual Studio generates 2 files: one import library (.lib) and
|
|
|
|
one - dynamic library (.dll). GCC (and mingw family) exports all symbols
|
|
> Visual Studio generates 2 files: one import library (.lib) and
|
|
by default, VS doesn't export anything by default. And if your dll
|
|
> one - dynamic library (.dll). GCC (and mingw family) exports all symbols
|
|
doesn't export anything, VS simply doesn't generate the import library
|
|
> by default, VS doesn't export anything by default. And if your dll
|
|
(for example, if you generate resource-only dll). The only thing cmake
|
|
> doesn't export anything, VS simply doesn't generate the import library
|
|
knows is when you using VS generator, library extension to link with --
|
|
> (for example, if you generate resource-only dll). The only thing cmake
|
|
is .lib. And when you use __declspec( dllexport ), cl.exe puts special
|
|
> knows is when you using VS generator, library extension to link with --
|
|
marks in objects indicating that this symbol should be exported or
|
|
> is .lib. And when you use `__declspec( dllexport )`, cl.exe puts special
|
|
imported. Also, VS can tell linker to add some library, as boost does
|
|
> marks in objects indicating that this symbol should be exported or
|
|
with \#pragma's. </i>
|
|
> imported. Also, VS can tell linker to add some library, as boost does
|
|
|
|
> with `#pragma`s.
|
|
|
|
|
|
-----
|
|
-----
|
|
|
|
|
|
So with all this information, how does all this work with CMake? There
|
|
So with all this information, how does all this work with CMake? There
|
|
are a few pieces of code that you will need to add to your project.
|
|
are a few pieces of code that you will need to add to your project.
|
|
|
|
|
|
- Let us first consider the CMake code. In your CMakeLists.txt file
|
|
Let us first consider the CMake code. In your `CMakeLists.txt` file you will have something along the lines of:
|
|
you will have something along the lines
|
|
|
|
of:
|
|
|
|
|
|
|
|
<!-- end list -->
|
|
|
|
|
|
|
|
# Allow the developer to select if Dynamic or Static libraries are built
|
|
# Allow the developer to select if Dynamic or Static libraries are built
|
|
OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
|
|
OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
|
|
# Set the LIB_TYPE variable to STATIC
|
|
# Set the LIB_TYPE variable to STATIC
|
|
SET (LIB_TYPE STATIC)
|
|
SET (LIB_TYPE STATIC)
|
|
IF (BUILD_SHARED_LIBS)
|
|
IF (BUILD_SHARED_LIBS)
|
|
# User wants to build Dynamic Libraries, so change the LIB_TYPE variable to CMake keyword 'SHARED'
|
|
# User wants to build Dynamic Libraries,
|
|
|
|
# so change the LIB_TYPE variable to CMake keyword 'SHARED'
|
|
SET (LIB_TYPE SHARED)
|
|
SET (LIB_TYPE SHARED)
|
|
ENDIF (BUILD_SHARED_LIBS)
|
|
ENDIF (BUILD_SHARED_LIBS)
|
|
|
|
|
... | @@ -75,12 +76,12 @@ This is generated by concatenating the name of the target 'MyLibrary' |
... | @@ -75,12 +76,12 @@ This is generated by concatenating the name of the target 'MyLibrary' |
|
with '_EXPORTS'.
|
|
with '_EXPORTS'.
|
|
Now we have to exploit that definition in our code so we need to add
|
|
Now we have to exploit that definition in our code so we need to add
|
|
some c code into a header that will get included by every class or file
|
|
some c code into a header that will get included by every class or file
|
|
in the project. A Good name for this might be <b>DLLDefines.h</b>.
|
|
in the project. A Good name for this might be `DLLDefines.h`.
|
|
Notice I did NOT call it Win32Exports.h as GCC 4.x series has some of
|
|
Notice I did NOT call it `Win32Exports.h` as GCC 4.x series has some of
|
|
these types of definitions that can be used and we should keep all this
|
|
these types of definitions that can be used and we should keep all this
|
|
in a single file (see [GCC
|
|
in a single file (see [GCC
|
|
Visibility](http://gcc.gnu.org/wiki/Visibility) ). So, what is the
|
|
Visibility](http://gcc.gnu.org/wiki/Visibility) ). So, what is the
|
|
contents of "DLLDefines.h" you might ask?
|
|
contents of `DLLDefines.h` you might ask?
|
|
|
|
|
|
// Contents of DLLDefines.h
|
|
// Contents of DLLDefines.h
|
|
#ifndef _MyLibrary_DLLDEFINES_H_
|
|
#ifndef _MyLibrary_DLLDEFINES_H_
|
... | @@ -130,12 +131,11 @@ as a dll, Visual Studio should create the proper set of libraries. |
... | @@ -130,12 +131,11 @@ as a dll, Visual Studio should create the proper set of libraries. |
|
|
|
|
|
-----
|
|
-----
|
|
|
|
|
|
<b>But wait\!</b> I have an existing project that I am bringing to cmake
|
|
**But wait!** I have an existing project that I am bringing to cmake
|
|
and it already has this type of code in place but the definition that
|
|
and it already has this type of code in place but the definition that
|
|
the project uses is different than what CMake produces, now what do I
|
|
the project uses is different than what CMake produces, now what do I
|
|
do? Say that project uses <b>COMPILING_DLL</b> as its definition, then
|
|
do? Say that project uses `COMPILING_DLL` as its definition, then
|
|
you can do the following in
|
|
you can do the following in CMake:
|
|
CMake:
|
|
|
|
|
|
|
|
SET_TARGET_PROPERTIES (MyLibrary PROPERTIES DEFINE_SYMBOL "COMPILING_DLL" )
|
|
SET_TARGET_PROPERTIES (MyLibrary PROPERTIES DEFINE_SYMBOL "COMPILING_DLL" )
|
|
|
|
|
... | | ... | |