2

I am trying to build the library, a SHARED library, with following CMakeLists.txt This is a simple learning example: https://github.com/petrasvestartas/CMAKE/tree/main/libraries

If I change the SHARED keyword to STATIC all works fine, but SHARED gives me a linking error.

What is causing this error, and how can I build those two command line projects that links a dynamic library?

LINK : fatal error LNK1104: cannot open file 'Debug\math_lib.lib' [C:\IBOIS57\_Code\Software\CPP\CMAKE\libraries\build\my_exe.vcxproj]

CMakeLists:

cmake_minimum_required(VERSION 3.0)
project(myproject LANGUAGES CXX)

# library name, library type STATIC / SHARED, and source files
add_library(math_lib SHARED math.cpp)

# you do not need to specify math.cpp files anymore, since they are in math_lib
add_executable(my_exe main.cpp)
add_executable(my_exe_2 main_2.cpp)

# link executables to the library, with a PUBLIC / PRIVATE / INTERFACE keyword
# there can be more than one library e.g. target_link_libraries(my_exe PUBLIC math_lib other_library_1 other_library_2)
target_link_libraries(my_exe PUBLIC math_lib)
target_link_libraries(my_exe_2 PUBLIC math_lib)

enter image description here

  • You need to **export** at least one symbol(function) from your `.dll` library, otherwise `.lib` file is not created and the library cannot be linked with. – Tsyvarev Jun 05 '22 at 21:40
  • Thank you for answer. Can you explain what do I need to do as an example? – PetrasVestartasEPFL Jun 05 '22 at 23:38
  • There is a duplicate banner at the top with the reference to the question. That question and its [answer](https://stackoverflow.com/a/40743080/3440745) describes how to overcome the error. – Tsyvarev Jun 06 '22 at 00:03
  • Still do not understand what is duplicated. – Petras Vestartas Jun 06 '22 at 05:09
  • The duplicate question is about the same error message, as you get (only name of the file differs) and about the same **reasoning** of such message: the created library has no exported symbols. – Tsyvarev Jun 06 '22 at 07:15

0 Answers0