11

I've got a library definition in CMake that builds a shared library out of a small set of files, and I've got it compiling just fine on both linux and windows.

However, I've also got another library that links against the shared library and it works fine on linux, however, on windows I get a message along the lines or "error can't find Release/nnet.lib" during link-time. Is there something special I have to do to get this to link on windows?

Edit, example:

Main shared library (filenames changed to protect the innocent):

ADD_LIBRARY(nnet SHARED
  src/nnet/file_1.cc src/nnet/file_3.cc  
  src/nnet/file_2.cc src/nnet/file_4.cc)

And then I'm building a python module that links in the library:

# Build python module
ADD_LIBRARY            (other_lib SHARED ${CMAKE_SOURCE_DIR}/src/boost/boost_main.cc)
TARGET_LINK_LIBRARIES  (other_lib nnet   ${PYTHON_LIBRARIES})

The rest is just boilerplate (eg: changing module extension to .pyd on windows, finding python libraries/headers, etc) And then when building in VS 2008 I get:

fatal error LNK1181: cannot open input file 'Release\nnet.lib'

when building other_lib. Note no errors are thrown while building nnet.

gct
  • 14,100
  • 15
  • 68
  • 107

1 Answers1

13

Ah, my problem was I forgot to include a __declspec(dllexport) in suitable places when building the library (can you tell I don't do windows programming a lot?).

gct
  • 14,100
  • 15
  • 68
  • 107
  • 2
    On windows no .lib import library file is generated if no symbols are exported. You need at least one dllexport for the file to be created. I have beaten my head against this before. – Christopher Bruns Feb 09 '10 at 23:11
  • 7
    Alternatively, you can also put a `set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)` into your `CMakeLists.txt` and then all symbols will be exported without needing `__declspec(dllexport)`. – josch Mar 12 '18 at 16:01