visual studio cl.exe link .lib files by path

1.4k Views Asked by At

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;
}
2

There are 2 best solutions below

0
Minxin Yu - MSFT On BEST ANSWER

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.

0
Gary Allen On

I found a way to find the missing libs location.

If you compile your code with cl.exe or clang.exe, if you have missing libs, it will display errors saying "error LNK2019: unresolved external symbol __imp_CLSIDFromString"

Then you use grep to search the "__imp_CLSIDFromString" symbol in all of the libs folders.

For example:

grep -o --color -a "__imp_CLSIDFromString" /mnt/c/Program\ Files\ \(x86\)/Windows\ Kits/10/Lib/10.0.22000.0/um/x64/*