0

I'm writing a simple lib, using cmake to do package management. Recently I come across a problem when link the lib to a test program. Here is a minimal cmake file.

PROJECT(hello)

add_library(hello  SHARED hello.cpp)

add_executable(hello_test hello_test.cpp)
target_link_libraries(hello_test hello)

when building the hello_test target, the hello_test tries to link hello.lib instead of hello.dll. But the hello target is shared, which doesn't generate hello.lib.

So my question is how to force the hello_test to link against hello.dll?

spiritsaway
  • 635
  • 1
  • 7
  • 16
  • 1
    It's been a while since I did Windows programming, but don't you always need a .lib import library for a .dll? From [the add_library docs](https://cmake.org/cmake/help/v3.5/command/add_library.html) it seems that you need to export at least one symbol in order for CMake to generate a .lib file. Also see https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/BuildingWinDLL. – Thomas Jan 17 '20 at 08:53
  • just use ```cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE``` solve my problem, thanks @Thomas – spiritsaway Jan 17 '20 at 08:58
  • Exporting _all_ symbols is rarely what you want though. In an actual project, you probably want to use `__declspec(dllimport)` and `__declspec(dllexport)` attributes generated through e.g. a `HELLO_EXPORT` macro instead (expand to export when building the DLL, and to import when building the main executable). – Thomas Jan 17 '20 at 09:00
  • Then what happens when I use cmake to generate both static and shared library, the hello.lib would be created twice. – spiritsaway Jan 17 '20 at 09:47
  • https://stackoverflow.com/questions/2152077/is-it-possible-to-get-cmake-to-build-both-a-static-and-shared-version-of-the-sam#2152157 – Thomas Jan 17 '20 at 14:32

1 Answers1

2

You always link with a .lib on Windows. If you do not have a lib, need to create it (google for: create lib for dll)

ddbug
  • 1,392
  • 1
  • 11
  • 25