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

Andrea Gualano andrea.gualano at imavis.com
Fri Nov 20 07:34:51 EST 2009


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



More information about the CMake mailing list