-2

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;
}
Gary Allen
  • 385
  • 1
  • 3
  • 11
  • 3
    ***Any solution?*** Your first example is the solution. You need to link to each individual library you use. Setting the directory to look for libs does not help. – drescherjm Apr 10 '23 at 03:10
  • 2
    The linker won't know which libraries to link to unless you specify the libraries to link. So your first example is the solution – Asesh Apr 10 '23 at 03:12
  • 3
    Imagine the library search path includes hundreds of libraries including multiple versions and debug/release versions. Would you really want it to try and figure out what to link? Even if it got it 100% right can you imagine how much time that might add to a build? If it bugs you make a batch file or a makefile so you only need to do it once. – Retired Ninja Apr 10 '23 at 03:44
  • Even if what you said is true, how do you know which libs should be specified? – Gary Allen Apr 10 '23 at 03:59
  • 2
    @GaryAllen Experience, documentation, tool defaults, experimentation. If you miss any out you'll get a linker error, then you can look that function up and find out what library it is part of. – john Apr 10 '23 at 06:48
  • In msdn if you go to the documentation for a header it tells you the library that is needed. Most of the time the defaults are all that are needed but sometimes you have to add additional entries. – drescherjm Apr 10 '23 at 12:56

2 Answers2

1

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.

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14
0

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/*
Gary Allen
  • 385
  • 1
  • 3
  • 11