Linking error when changing link libraries in Visual Studio
Let's say you have the following minimal CMakeLists.txt
:
cmake_minimum_required(VERSION 3.18)
project(vstest C)
add_executable(exe main.c)
if(0) # We will enable this later
add_library(mylib STATIC mylib.c)
target_link_libraries(exe mylib)
endif()
If you build this with Visual Studio, it of course builds the exe
executable and does not build mylib
because it has not been enabled. This works as expected.
Now, if you enable the if()
statement, CMake regenerates, and tries to link exe
against mylib
. However, this fails at the same time that you are asked to reload the solution:
This is happening because the solution has not been reloaded, which is required for mylib
to build. Reloading the solution and building again fixes the issue, but this can be confusing for users.
For obvious reasons, I don't know if there's much we can do about this.
Edited by Kyle Edwards