Visual Studio toolchain linker can't find library generated in the project
Hi, I am new to CLion and back to C++ after a long stint. I am just playing with CLion to try to understand (and refresh my memory) how to link a project. I started with an Hello World project and in order to complicate things a bit I am building a library LbxDM with the declaration of the hello world function and an executable LbxRun that calls it.
I am using CLion 2022.3 (Build Nov. 2022).
On my windows machine I am trying to use two toolchains, MinGW and Visual Studio. With MinGW all goes fine. With Visual Studio I have problem linking the executable to the library. Here is the error:
====================[ Build | all | Debug-Visual Studio ]=======================
C:\XXX\cmake.exe --build E:\YYY\LbxDM\cmake-build-debug-visual-studio --target all -j 19
[1/4] Building CXX object CMakeFiles\LbxRun.dir\run.cpp.obj
[2/4] Building CXX object CMakeFiles\LbxDM.dir\library.cpp.obj
[3/4] Linking CXX shared library LbxDM.dll
[4/4] Linking CXX executable LbxRun.exe
FAILED: LbxRun.exe
cmd.exe /C "cd . && C:\XXX\bin\cmake\win\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\LbxRun.dir
--rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\rc.exe
--mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x86\mt.exe
--manifests
-- C:\ZZZ\VC\Tools\MSVC\1434~1.319\bin\Hostx86\x86\link.exe
/nologo CMakeFiles\LbxRun.dir\run.cpp.obj /out:LbxRun.exe /implib:LbxRun.lib /pdb:LbxRun.pdb
/version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console LbxDM.lib
E:\YYY\boost\boost_1_80_0\stage\lib\boost_date_time-vc143-mt-gd-x32-1_80.lib
kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib c
omdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\ZZZ\VC\Tools\MSVC\1434~1.319\bin\Hostx86\x86\link.exe
/nologo CMakeFiles\LbxRun.dir\run.cpp.obj
/out:LbxRun.exe /implib:LbxRun.lib /pdb:LbxRun.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL
/subsystem:console LbxDM.lib
E:\YYY\boost\boost_1_80_0\stage\lib\boost_date_time-vc143-mt-gd-x32-1_80.lib
kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib
uuid.lib comdlg32.lib advapi32.lib /MANIFEST
/MANIFESTFILE:CMakeFiles\LbxRun.dir/intermediate.manifest
CMakeFiles\LbxRun.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'LbxDM.lib'
ninja: build stopped: subcommand failed.
The build on MinGW goes fine:
====================[ Build | all | Debug ]=====================================
C:\XXX\cmake.exe --build E:\YYY\LbxDM\cmake-build-debug --target all -j 19
[1/4] Building CXX object CMakeFiles/LbxRun.dir/run.cpp.obj
[2/4] Building CXX object CMakeFiles/LbxDM.dir/library.cpp.obj
[3/4] Linking CXX shared library libLbxDM.dll
[4/4] Linking CXX executable LbxRun.exe
The main difference I see here is that the Visual Studio toolchain build the shared library (as requested) and tries to link the executable to the static library. I'd assume that there is something I am missing in the configuration of the Visual Studio toolchain but I can't get my head around it.
For completeness here below is the CMakeLists.txt file of the project, admittedly a bit oversized for the task at hand as I am also checking the installation of boost, but it should not be the problem.
cmake_minimum_required(VERSION 3.23)
project(LbxDM)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_CXX_STANDARD 20)
set(BOOST_REQUIRED_VERSION 1.80)
set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost COMPONENTS regex date_time)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIR: ${Boost_LIBRARY_DIR}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_library(LbxDM SHARED library.cpp)
target_link_libraries(LbxDM ${Boost_DATE_TIME_LIBRARY} )
add_executable(LbxRun run.cpp)
target_link_libraries(LbxRun LbxDM)
Please sign in to leave a comment.
Hello!
This question is related to CMake, not CLion.
I found the following discussion, for example (quite old, to be honest, but maybe still relevant) - https://cmake.cmake.narkive.com/k3wBWwfi/how-to-link-against-a-dll-with.