[CMake] Link and build to DLL under Windows with NMake

Michael Wild themiwi at gmail.com
Fri Nov 20 08:49:12 EST 2009


Also make sure to use unique macro names for every library. Say you  
want to build two DLL's, foo and bar:

// foo/foo.h
#ifndef _foo_h
#define _foo_h

#ifdef WIN32
#  ifdef foo_EXPORTS
#    define FOO_EXPORT __declspec(dllexport)
#  else
#    define FOO_EXPORT __declspec(dllimport)
#  endif
#else
#  define FOO_EXPORT
#endif

class FOO_EXPORT fooClass {/* some code */};

#endif // _foo_h


// bar/bar.h
#ifndef _bar_h
#define _bar_h

#include <foo/foo.h>

#ifdef WIN32
#  ifdef bar_EXPORTS
#    define BAR_EXPORT __declspec(dllexport)
#  else
#    define BAR_EXPORT __declspec(dllimport)
#  endif
#else
#  define BAR_EXPORT
#endif

class BAR_EXPORT barClass : public fooClass {/* some code */};

#endif // _bar_h

If you'd chosen simply EXPORT instead of FOO_EXPORT and BAR_EXPORT,  
you'd be in real trouble when it comes to building bar.


On 20. Nov, 2009, at 13:57 , ggrimm at detec.de wrote:

> Hello Andrea,
>
> the linker didn't create hello.lib, because you didn't export anything
> from hello.dll.
>
> Here's the usual way to do it:
> When compiling sources of a shared library <target>, CMake will  
> declare a
> preprocessor symbol <target>_EXPORTS.
> You can use this preprocessor symbol to decide whether you want to  
> export
> or import a symbol while compiling:
>
> // src/hello.h
> #ifndef _hello_h
> #define _hello_h
>
> #ifdef WIN32
> #  ifdef hello_EXPORTS
> #    define EXPORT __declspec(dllexport)
> #  else
> #    define EXPORT __declspec(dllimport)
> #else
> #  define EXPORT
> #endif
>
> class EXPORT Hello {
> public:
>   void sayHello();
> };
>
> #endif
>
> That should do the trick. Make sure that WIN32 is among your default
> preprocessor symbols on Windows.
>
> Best regards,
>
> Gerhard
>
>
>
> From:
> Andrea Gualano <andrea.gualano at imavis.com>
> To:
> cmake at cmake.org
> Date:
> 20.11.2009 13:35
> Subject:
> [CMake] Link and build to DLL under Windows with NMake
> Sent by:
> cmake-bounces at cmake.org
>
>
>
> Hello,
> I am trying to build a .dll file under Windows and then build some  
> test
> executable just to be sure that it works correctly.
> The source is C++, and I am trying to link to a class defined inside  
> the
> shared library.
> This is probably very basic, but I know very little about Windows
> development and Google didn't help me.
>
> This is my test project (full source and CMakeLists):
>
> # top level CmakeLists.txt
> PROJECT(HELLO)
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> ADD_SUBDIRECTORY(src)
> ADD_SUBDIRECTORY(test)
>
> # src/CmakeLists.txt
> ADD_LIBRARY(hello SHARED hello.cpp)
>
> // src/hello.cpp
> #include "hello.h"
> #include <iostream>
>
> void Hello::sayHello() {
>   std::cout << "Hello, world!" << std::endl;
> }
>
> // src/hello.h
> #ifndef _hello_h
> #define _hello_h
>
> class Hello {
> public:
>   void sayHello();
> };
>
> #endif
>
> # test/CmakeLists.txt
> INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/src)
> ADD_EXECUTABLE(test test.cpp)
> TARGET_LINK_LIBRARIES(test hello)
>
> // test.cpp
> #include "hello.h"
>
> int main() {
>   Hello().sayHello();
> }
>
>
> This project builds correctly under Linux, but when I try to build it
> under Windows with NMake, I get the following:
>
> Scanning dependencies of target hello
> [ 50%] Building CXX object src/CMakeFiles/hello.dir/hello.cpp.obj
> hello.cpp
> Linking CXX shared library hello.dll
> [ 50%] Built target hello
> Scanning dependencies of target test
> [100%] Building CXX object test/CMakeFiles/test.dir/test.cpp.obj
> test.cpp
> NMAKE : fatal error U1073: don't know how to make 'src\hello.lib'
> Stop.
> NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio
> 9.0\VC\BIN\nmake.exe"' : return code '0x2'
> Stop.
> NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio
> 9.0\VC\BIN\nmake.exe"' : return code '0x2'
> Stop.
>
> That is, the DLL is built correctly but it can't be linked to because
> there is no .lib file.
> So the question is: how do I create a .lib file so that the executable
> can be built?
>
> The project builds if I say STATIC instead of SHARED, though what I  
> want
> is a DLL.
>
> Thanks and best regards,
> Andrea
>
> -- 
> Andrea Gualano
> ImaVis S.r.l.
> email: andrea.gualano at imavis.com
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list