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

ggrimm at detec.de ggrimm at detec.de
Fri Nov 20 07:57:42 EST 2009


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20091120/adef45ac/attachment.htm>


More information about the CMake mailing list