0

I have a simple c++ program with following structure. I use cmake in visual studio to build it.

Project Description:

  • There is a root level which has main file named module.cpp along with module.h.
  • Root has a subfolder src which has two other folders iMath and utils.
  • utils folder has a myprint.h as well as myprint.cpp
  • iMath is built as library. it has 2 folders inside it, include folder has a header file addition.h while src folder has a addition.cpp.

Problem detail:

When iMath is build as a static library, it builds well. However, when it is built as shared library, it results in LNK1104 cannot open file src\iMath\MathLib.lib error.

enter image description here

CMakeLists.txt at Root level:

cmake_minimum_required (VERSION 3.8)
project (module4)
set(UTILS_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/utils)

# add subdirectory
add_subdirectory(src)


# Add source to this project's executable.

add_executable (
calculator
module.cpp 
${UTILS_SOURCE_DIR}/myprint.cpp
)

target_include_directories(calculator PUBLIC ${UTILS_SOURCE_DIR})

target_link_libraries(calculator PUBLIC iMathLib)

CmakeLists.txt at src level:

cmake_minimum_required (VERSION 3.8)
add_subdirectory(iMath)

CMakeLists.txt at iMath level:

cmake_minimum_required (VERSION 3.8)

project(iMath)

set(SRC_FILES src/addition.cpp )

add_library(iMathLib SHARED ${SRC_FILES})

target_include_directories(iMathLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

module.h at ROOT level

#pragma once

#include <iostream>
#include <string>
#include "addition.h"
#include "myprint.h"

In the last CMakeLists.txt at iMATH level, if i switch SHARED TO STATIC, everything works well.

Can someone please point me in the right direction as to what should i change so that it builds with iMath as shared as well?

toing
  • 466
  • 1
  • 3
  • 19
  • 1
    Why does it need to be shared? – sweenish Mar 15 '21 at 15:19
  • 1
    `set(PROJECT_NAME module4) project (${PROJECT_NAME})` `project()` sets `PROJECT_NAME` by itself, no need to set it manually. Just `project(module4)` – KamilCuk Mar 15 '21 at 15:20
  • @sweenish I would like to create a dll that can then be used in other projects outside of this one – toing Mar 15 '21 at 15:24
  • @KamilCuk Thanks, i will make those changes – toing Mar 15 '21 at 15:24
  • 2
    Echoing @KamilCuk, it's considered good practice to not name your project and executable the same thing. Quoting **Professional Cmake**: "This only works for the most basic of projects and encourages a number of bad habits." – sweenish Mar 15 '21 at 15:24
  • @sweenish - I have changed based on your suggestions. Original problem persists but its more readable now – toing Mar 15 '21 at 15:32
  • 1
    It seems you forget to **export symbols** from your library. On Windows it is required for being able to use the resulted shared library. – Tsyvarev Mar 15 '21 at 15:51
  • @Tsyvarev - I added following line set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) in CMakeLists.txt at iMath level. Now cmake as well as build goes well. However, it gives a runtime error that iMathLib.dll not found. Can you pls further point in right direction. – toing Mar 15 '21 at 16:15
  • 1
    For being able to load `.dll` at runtime, either the directory with that dll should be listed in the `PATH` variable, or this dll should be located near the executable which uses it. If you choose the last way, then googling for "cmake copy dll" gives you a lot of possibilities. – Tsyvarev Mar 15 '21 at 16:19
  • @Tsyvarev: i added following in root CmakeLists.txt and it works now. Thanks. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) – toing Mar 15 '21 at 16:42

0 Answers0