Overview
- I want to compile a project CalcMod using a shared library XMMathLib.
- Both should be loaded as projects in Visual Studio (ie in the Solution Explorer, there should be a tree for CalcMod, and a tree for XMMathLib). Hence nesting XMMathLib within CalcMod (and adding it with
add_subdirectory()
) seems to be the apropriate solution. - Also, eventually, we want to export XMMathLib as a (shared) dll library, hence we call
add_library(XMMathLib SHARED […])
. - However, this triggers a
cannot open file 'XMMathLib\Debug\XMMathLib.lib'
error at the end of compilation.
Indeed, the folderbuild\XMMathLib\Debug\
containsXMMathLib.dll
,XMMathLib.ilk
andXMMathLib.pdb
, which is consistent with me asking to generate a shared library for XMMathLib. But then, why does CalcMod look for static library fileXMMathLib.lib
?!
Details
Here is how the project is organised:
|- build
|- src
|- CalcMod
| |- src
| |- main.cpp
|- XMMathLib
| |- src
| | |- Demo.cpp
| | |- Demo.h
| |- CMakeLists.txt
|- CMakeLists.txt
And here is the content of each file:
src/CMakeLists.txt
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
project(CalcMod CXX)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(XMMathLib)
file(GLOB_RECURSE SOURCES_CALCMOD "CalcMod/src/*.cpp")
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES_CALCMOD})
add_executable(
CalcMod
${SOURCES_CALCMOD}
)
target_link_libraries(
CalcMod
XMMathLib
)
target_include_directories(CalcMod PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
src/XMMathLib/CMakeLists.txt
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
project(XMMathLib CXX)
file(GLOB_RECURSE SOURCES "src/*.cpp")
add_library(XMMathLib SHARED ${SOURCES})
target_link_libraries(
XMMathLib
)
src/CalcMod/src/main.cpp
#include <iostream>
#include "XMMathLib/src/Demo.h"
int main()
{
RunDemo();
}
src/XMMathLib/src/Demo.cpp
#include <iostream>
void RunDemo()
{
std::cout << "hello" << std::endl;
}
src/XMMathLib/src/Demo.h
#pragma once
void __declspec(dllexport) RunDemo();
cmake parameters:
- Visual Studio 2017
- Platform: Win32
- Windows SDK 10.0.19041.0