unable to load c2.dll when importing a static library

Windows specific forum
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

unable to load c2.dll when importing a static library

Post by Quin »

I'm attempting to link against libgit2 (https://github.com/libgit2/libgit2) in my app. I cloned it, built it as a static library with CMake, put it in my PureLibraries/Windows/Libraries folder, and have this code:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
	ImportC "git2.lib"
		git_libgit2_init()
	EndImport
	
	git_libgit2_init()
CompilerEndIf
However, when attempting to run this, I get:
git2.lib(libgit2.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
LINK : fatal error LNK1171: unable to load c2.dll (error code: 126)
So that's fine, I make a linker options file with /LTCG in it, and use it. Gets rid of that error, but this one still remains:
LINK : fatal error LNK1171: unable to load c2.dll (error code: 126)
I'm utterly lost on what to do here. I have no idea what c2.dll is. Googling was inconclusive, I found a few references to it in Everything search, but haven't been able to figure it out. SO I turn to you, amazing community! :) What does this mean, and how can I fix it?
Thanks in advance for any help!
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: unable to load c2.dll when importing a static library

Post by fryquez »

c2.dll is an optional dependency of link.exe. Final release of PB 6.10 remove the VC linker and switch to polink.exe again.

Either reinstall PB or grab the c2.dll (and properly more dlls) from a Visual Studio installation:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: unable to load c2.dll when importing a static library

Post by Quin »

I reverted back to polink, it's just easier. However, now I get this:
error: Unsupported anonymous format in object 'C:\Users\Quin\code3\libgit2\build\src\libgit2\libgit2.dir\Release\libgit2.obj'.

:twisted:
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: unable to load c2.dll when importing a static library

Post by Justin »

Hi,
i tried to build with mingw, it creates the satic lib but when calling git_libgit2_init() polink shows no errors but the debugger quits unexpectedly.

i thought that with the new linker these things would be easier but not in this case.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: unable to load c2.dll when importing a static library

Post by chi »

@Quin: In VisualStudio you have to set "Whole Program Optimization" to No, or remove /GL in cmake to make static libs work in PB
Et cetera is my worst enemy
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: unable to load c2.dll when importing a static library

Post by Quin »

chi wrote: Tue Apr 09, 2024 8:10 pm @Quin: In VisualStudio you have to set "Whole Program Optimization" to No, or remove /GL in cmake to make static libs work in PB
Thanks, this made it link properly, and also shrank the .lib by like 70 MB :shock:
And now, this happens. Guess the world really does not want me linking against libgit :(
---------------------------
PureBasic - Linker error
---------------------------
error: Unresolved external symbol 'close' - referenced from 'git2.lib(mwindow.obj)'.

POLINK: error: Unresolved external symbol '__imp_WinHttpQueryAuthSchemes' - referenced from 'git2.lib(winhttp.obj)'.

POLINK: error: Unresolved external symbol '__imp_CoInitializeEx' - referenced from 'git2.lib(winhttp.obj)'.

POLINK: error: Unresolved external symbol 'IID_IInternetSecurityManager' - referenced from 'git2.lib(winhttp.obj)'.

POLINK: error: Unresolved external symbol 'CLSID_InternetSecurityManager' - referenced from 'git2.lib(winhttp.obj)'.

POLINK: error: Unresolved external symbol '__imp_CoCreateInstance' - referenced from 'git2.lib(winhttp.obj)'.

POLINK: error: Unresolved external symbol '__imp_CoUninitialize' - referenced from 'git2.lib(winhttp.obj)'.

...


---------------------------
OK
---------------------------
So seemingly it's not including some COM component or something. I have no idea, this is not what I do.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: unable to load c2.dll when importing a static library

Post by chi »

You have to tell the linker where to find those symbols like this...

Code: Select all

Import "Ole32.lib" : EndImport
Import "Winhttp.lib" : EndImport
...
or include them in the static lib in VS ;)
Et cetera is my worst enemy
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: unable to load c2.dll when importing a static library

Post by chi »

I managed to get git_libgit2_init() running with...

Code: Select all

CompilerIf #PB_Compiler_IsMainFile

  Import "ole32.lib" : EndImport
  Import "winhttp.lib" : EndImport
  Import "crypt32.lib" : EndImport
  Import "wbemuuid.lib" : EndImport
  Import "rpcrt4.lib" : EndImport
  Import "zlib.lib" : EndImport
  Import "uuid.lib" : EndImport
  Import "shlwapi.lib" : EndImport
  
  Import ".\pcre.lib" : EndImport
  Import ".\oldnames.lib" : EndImport
  Import ".\git2.lib"
    git_libgit2_init()
  EndImport
  
  Debug git_libgit2_init()
  
CompilerEndIf
You have to compile pcre.lib (Perl Compatible Regular Expressions) yourself and also copy oldnames.lib from VisualStudio\VC\Tools\MSVC\xx.xx.xxxxx\lib\x64\uwp to the directory where the git2.lib is and you saved above .pb file.
Et cetera is my worst enemy
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: unable to load c2.dll when importing a static library

Post by Quin »

Wow, thanks so much, @chi! You're a legend!
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: unable to load c2.dll when importing a static library

Post by chi »

np, thx
Et cetera is my worst enemy
Post Reply