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