I am trying to build a cross-platform project using CMake and Visual C++ 2017 toolchain.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(CMakeLibTest)
add_executable(mainApp App.cpp)
target_include_directories(mainApp PRIVATE ${PROJECT_SOURCE_DIR}/../Lib)
target_link_libraries(mainApp -L${PROJECT_SOURCE_DIR}/../Win32/Debug -lLib)
Lib.lib
is some static library. It is located in the folder ../Win32/Debug
relative to the location of CMakeLists.txt and App.cpp.
When I start the project build I see strange options in the linker command line:
-LC:/Users/UserName/source/repos/CMakeLibTest/App/../Win32/Debug -lLib.lib
The linker cannot recognize these options and shows warnings:
warning LNK4044: unrecognized option '/LC:/Users/UserName/source/repos/CMakeLibTest/App/../Win32/Debug'; ignored
warning LNK4044: unrecognized option '/lLib.lib'; ignored
And finally it fails:
error LNK2019: unresolved external symbol "void __cdecl f(void)" (?f@@YAXXZ) referenced in function main
Expected correct linker command line options:
/LIBPATH:"C:\Users\UserName\source\repos\CMakeLibTest\Win32\Debug\" "Lib.lib"
What I am doing wrong? What is the correct way to link libraries in CMake compatible with Visual Studio? Or maybe it is a bug in CMake?