通过CMake构建时如何避免链接不必要的Windows库?

时间:2019-11-22 23:48:44

标签: c++ c cmake

我注意到,在Windows上使用CMake进行构建时,它会自动为所有项目添加一些不必要的库。

示例(部分ldd输出):

$ ldd ./build/release/bin/program.exe
        ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffee1180000)
        KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffee0d00000)
        KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffede3b0000)
        msvcrt.dll => /c/WINDOWS/System32/msvcrt.dll (0x7ffee0120000)
        libstdc++-6.dll => /c/mingw64/mingw64/bin/libstdc++-6.dll (0x6fc40000)
        mcfgthread-12.dll => /c/mingw64/mingw64/bin/mcfgthread-12.dll (0x6c080000)
        libboost_filesystem-mt.dll => /c/mingw64/mingw64/bin/libboost_filesystem-mt.dll (0x638c0000)
        ADVAPI32.dll => /c/WINDOWS/System32/ADVAPI32.dll (0x7ffedf230000)
        sechost.dll => /c/WINDOWS/System32/sechost.dll (0x7ffee0ef0000)
        RPCRT4.dll => /c/WINDOWS/System32/RPCRT4.dll (0x7ffee0230000)
        libboost_program_options-mt.dll => /c/mingw64/mingw64/bin/libboost_program_options-mt.dll (0x678c0000)
        libgcc_s_seh-1.dll => /c/mingw64/mingw64/bin/libgcc_s_seh-1.dll (0x61440000)
        WS2_32.dll => /c/WINDOWS/System32/WS2_32.dll (0x7ffee01c0000)
        WSOCK32.dll => /c/WINDOWS/SYSTEM32/WSOCK32.dll (0x7ffedbfd0000)
        libcrypto-1_1-x64.dll => /c/mingw64/mingw64/bin/libcrypto-1_1-x64.dll (0x67e00000)
        USER32.dll => /c/WINDOWS/System32/USER32.dll (0x7ffee0350000)
        win32u.dll => /c/WINDOWS/System32/win32u.dll (0x7ffede190000)
        GDI32.dll => /c/WINDOWS/System32/GDI32.dll (0x7ffee04f0000)
        gdi32full.dll => /c/WINDOWS/System32/gdi32full.dll (0x7ffede1c0000)
        msvcp_win.dll => /c/WINDOWS/System32/msvcp_win.dll (0x7ffedee30000)
        ucrtbase.dll => /c/WINDOWS/System32/ucrtbase.dll (0x7ffedeed0000)
        libssl-1_1-x64.dll => /c/mingw64/mingw64/bin/libssl-1_1-x64.dll (0x6d480000)

我的编译器(带有MCF线程和SEH的MinGW-w64)的CMAKE_C_STANDARD_LIBRARIESCMAKE_CXX_STANDARD_LIBRARIES的默认值为-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32。对于控制台应用程序,我非常确定它不需要gdi32和其他许多应用程序。 是否有任何方法可以在不触碰CMAKE_变量的情况下禁用这些库的链接?在现代CMake和基于目标的配方时代,我想避免触碰任何变量。

0 个答案:

没有答案