Issue
I'll use CMake's example project as an example. So I have this:
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.h)
set_target_properties(Tutorial PROPERTIES LINKER_LANGUAGE CXX)
After I generate the solution, when I open the solution in Visual Studio and go to Project Properties - Configuration Properties - Linker - Input - Additional Dependencies
, I see that it links a lot of libraries :
I'd like to prevent user32.lib from linking for this specific project(not for every project in the solution). I tried googling and found this thread: How to avoid linking to system libraries. But I couldn't find a solution.
The reason I'd like to do this is because I'm trying to not link user32.lib in my test project, so I can do the link substitution(also known as link seam) technique to be able to provide my own implementation in the test project, to mock the system calls to be able to test classes that do these system calls.
It already works: I removed the library in the Visual Studio's project properties(as well as added it to the list in the Ignore Specific Default Libraries
property), but the problem is that every time the solution is regenerated, the linking of the library gets restored.
Solution
Posting this question and the answer because I couldn't find the solution on google, and it seems there wasn't one.
Ok, so to fix the problem, all I need to do is:
SET(CMAKE_CXX_STANDARD_LIBRARIES "")
And that's it! Now CMake won't explicitly link all the libraries in the first screenshot in the question.
But Even after setting this line to be empty, the whole solution keeps working: it actually still links all those libraries because of %(AdditionalDependencies)
and the Inherit from parent or projects defaults
checkbox:
And now to actually exclude the user32.lib from linking in my the project, I do:
SET_TARGET_PROPERTIES(${MY_PROJECT_NAME} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:User32.lib")
Answered By - KulaGGin Answer Checked By - Mary Flores (WPSolving Volunteer)