-1

I have this cmake:

cmake_minimum_required (VERSION 3.20)
project (DllTest)
if (MSVC)
    add_compile_definitions(UNICODE _UNICODE)
endif()


add_library(mydll SHARED dllmain.cpp)


add_executable(test_dll testmain.cpp)
target_link_libraries(test_dll PUBLIC mydll)

when I generate projects using this cmake test file and compile the code, I am getting this error:

Error   LNK1104 cannot open file 'Debug\mydll.lib'      

I searched all of the build directories and I can not find any mydll.lib.

How can I create a Dll and linked it to another project using cmake?

starball
  • 20,030
  • 7
  • 43
  • 238
mans
  • 17,104
  • 45
  • 172
  • 321
  • Show your code (see [mre]). Did you export any symbols from your ddlmain header file? What version of MSVC are you using? Have you tried using [the `dir /s` command to search for the file](/q/8066679)? – starball Feb 10 '23 at 01:16
  • 1
    If you don't export symbols from your code for the dll you don't get an import library. This may help: [https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170) – drescherjm Feb 10 '23 at 01:42
  • @Tom what on earth does that have to do with this link error? – starball Feb 10 '23 at 01:42
  • Once you've provided your [mre], if the problem is really that you haven't exported anything, I can write up an answer post. Tom's answer will work, but I'd suggest exporting symbols "case-by-case", which CMake can help you out with via its [`GenerateExportHeader`](https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html) module. – starball Feb 10 '23 at 05:02
  • @user The problem as you mentioned was that I did not export anything. By exporting some functions, the problem is solved, So if you create an answer based on your comment, then I will accept it. – mans Feb 19 '23 at 19:44
  • I can't answer and don't need to answer because another Q&A should already sufficiently answer this question (this has been closed as a duplicate) – starball Feb 19 '23 at 19:49

1 Answers1

0

According to Create dlls on Windows without declspec() using new CMake export all feature, the author said:

A shared library on Windows known as a DLL (dynamic linked library) requires changes to the source code or an explicit listing of all the symbols that the dll will export. The compilers on Linux/UNIX have the ability to export all symbols in a shared library automatically. On Windows, you must either use compiler directives __declspec(import) and __declspec(export) to declare which symbols are exported/imported from a shared library, or you must create a module definition text file (.def) with a list of all the symbols you want to export and pass that file to the linker. The feature is implemented in CMake via a new target property, WINDOWS_EXPORT_ALL_SYMBOLS. When enabled, this property causes CMake to automatically create a .def file with all symbols found in the input .obj files for a SHARED library on Windows. The .def file will be passed to the linker causing all symbols to be exported from the DLL.

So add set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) in your CMakeLists or run cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE -DBUILD_SHARED_LIBS=TRUE , the link error will be fixed.

Tom
  • 177
  • 7
  • 2
    That's one way to do it :/ but not the only way. Exporting unnecessary symbols has size impact and could be considered a form of breaking encapsulation. And the asker hasn't even provided enough clarification to be sure that this is the problem (although I'd wager it is). Maybe be a little less trigger happy? – starball Feb 10 '23 at 01:52
  • 1
    Don't do that, `CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS` is a workaround for existing libraries which don't properly handle visibility of their symbols, not a proper long term solution. – SpacePotatoes Feb 11 '23 at 14:11