Monday, July 11, 2022

[SOLVED] Why CMake adds unnecessary libraries to Visual studio project?

Issue

I have this simple CMake file

cmake_minimum_required(VERSION 2.8)
project(test)
set(SOURCES source.cpp)
add_executable(test ${SOURCES})

where source.cpp is a simple hello world program. I then generate the Visual Studio project

cmake -G"Visual Studio 14 2015" ..\Sources  

The generated Visual Studio project has the following libraries, under Configuration Properties > Linker > Input > Additional Dependencies:

kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib

If I remove these libraries I can still successfully build and run the hello world.

Why does CMake add all these libraries and what can I do to not have them in my projects?


Solution

As commented by @Vertexwahn those are the defaults defined from CMake by CMAKE_CXX_STANDARD_LIBRARIES_INIT in CMakeCXXInformation.cmake.

I just wanted to add a simple AdditionalDependencies macro replacement found here:

cmake_minimum_required(VERSION 3.0)

file(
    WRITE "${CMAKE_BINARY_DIR}/MakeRulesOverwrite.cmake"
[=[
    if (MSVC)        
        set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "%(AdditionalDependencies)")
    endif()
]=]
)
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_BINARY_DIR}/MakeRulesOverwrite.cmake")

project(test)
...

References



Answered By - Florian
Answer Checked By - Terry (WPSolving Volunteer)