Search code examples
cwindowscmaketoolchain

How do I make CMake find default system libraries to link against without manual script manipulation?


I am trying to build a CMake project on my new pc, I have installed the appropriate Windows 10 Dev kit. But when trying generate the build files using cmake, I am getting linking errors by the cmake's internal compiler testing scripts, says:

-- Check for working C compiler: X:/xx/xx/LLVM/bin/clang.exe - broken
CMake Error at D:/BuildTools/CMake/share/cmake-3.30/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "X:/xx/xx/LLVM/bin/clang.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'X:/x-projects/cmake-initial/build/CMakeFiles/CMakeScratch/TryCompile-ywq7mz'

    Run Build Command(s): X:/BuildTools/Ninja/ninja.exe -v cmTC_f9821
    [1/2] <A long list of commands/subcommands invoked by cmake>
    [2/2] <A long list of commands/subcommands invoked by cmake>
    FAILED: cmTC_f9821.exe
    
    lld-link: error: could not open 'kernel32.lib': no such file or directory
    lld-link: error: could not open 'user32.lib': no such file or directory
    lld-link: error: could not open 'gdi32.lib': no such file or directory
    lld-link: error: could not open 'winspool.lib': no such file or directory
    ...list goes on...

    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

Note: I have reduced the the error log items that weren't necessary and doing so makes it easier to read.

Now as you see, it is unable to link against the libraries included in my windows dev kit. I have tried adding the library directory inside Windows Dev Kit to the PATH. Still doesn't work!
I am not using Visual Studio and MSVC toolchain, I am using CMake with Ninja and Clang toolchain. I don't want to manually modify that compiler testing scripts because it creates more mess.

EDIT:
So the question gets reduced down to: How do I make sure CMake find these system libraries to link without making any changes to the build scripts (CMakeLists.txt), (Automatically finding the system libraries)?


Solution

  • Okay, I got that the linker wasn't able to find the necessary Windows Dev Kit libraries to link because the subcommand generated by cmake compiler test scripts doesn't contain the path to these libraries.
    The simple solution to this is to create an environment variable called LIB, containing the path to those libraries' folder, In my case:
    C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64

    (; used to separate both paths) This made the linker to look into these folders and grab the necessary libraries.