Why CMake adds unnecessary libraries to Visual studio project?


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?

- - Source

Answers

answered 2 year ago Florian #1

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

comments powered by Disqus