c++windowsvisual-studiovisual-c++cl.exe

visual studio cl.exe link .lib files by path


Currently I compile and link c++ program like this

cl.exe /EHsc main.cpp /link kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

It looks very awkward, I tried this

cl.exe /EHsc main.cpp /link /LIBPATH:"D:\Windows Kits\10\Lib\10.0.19041.0\um\x64"

It did not work. you have to specify each .lib file individually.

Any solution?

PS: my sample main.cpp code

#include <Windows.h>
#include <iostream>

int main()
{
    SetCursorPos(1000, 1000);
    std::cout << "Hello World!\n";
    return 0;
}

Solution

  • The library name must be specified explicitly in the linker.

    If you check Linker option-> Marco in Visual Studio, you will find that there is marco $(CoreLibraryDependencies);%(AdditionalDependencies).

    And you can do something like this in Developer Powershell for Visual Studio(the tool can be found in Win->Visual Studio folder).

    $CoreLibraryDependencies=@("kernel32.lib","user32.lib","gdi32.lib","winspool.lib","comdlg32.lib","advapi32.lib","shell32.lib","ole32.lib","oleaut32.lib","uuid.lib","odbc32.lib","odbccp32.lib")
    

    And

     cl.exe /EHsc main.cpp /link $CoreLibraryDependencies /LIBPATH:"D:\Windows Kits\10\Lib\10.0.19041.0\um\x64"
    

    Of course, it is also possible to store all libs in the folder into array via powershell script.