Visual Studio linker download

Windows specific forum
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Visual Studio linker download

Post by ChrisR »

It makes sense, in Fryquez's libs downloader, I'm missing msvcprt.lib
and if I comment on Import "msvcprt.lib" and keep libcpmt.lib in link parameters, I get error LNK2001: unresolved external symbol WinMainCRTStartup,...
I didn't go any further yet.
It's interesting, I hope Fred will add an option for dynamic ucrt in future.
In any case, the MS visual studio linker has been adopted by me, it really helps with exe size.
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Visual Studio linker download

Post by fryquez »

If I think on how many place I used NODEFAULTLIB:libname :shock:

Update lib download for msvcrt.lib and msvcprt.lib.
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Visual Studio linker download

Post by ChrisR »

Thanks for the added libs, so I gave it another try:
chi wrote: Mon Apr 29, 2024 12:36 am

Code: Select all

Import "ucrt.lib"      : EndImport  ;; libucrt.lib       The Universal CRT (UCRT) contains the functions and globals exported by the standard C99 CRT library.
Import "vcruntime.lib" : EndImport  ;; libvcruntime.lib  The vcruntime library contains Visual C++ CRT implementation-specific code: exception handling and debugging support, runtime checks and type information, implementation details, and certain extended library functions.
Import "msvcrt.lib"    : EndImport  ;; libcmt.lib        This code handles CRT startup, internal per-thread data initialization, and termination.      
Import "msvcprt.lib"   : EndImport  ;; libcpmt.lib       C++ standard library (STL) .lib files
By removing the 4 libs from the linker libucrt.lib, libvcruntime.lib, libcmt.lib and libcpmt.lib,
I have a mismatch error detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in pbscintilla.LIB

It seems to work if I remove libucrt.lib, libvcruntime.lib and libcmt.lib
And for libcpmt.lib, move it, just before the imported msvcprt.lib. Don't know why but the order is important here!
Seems to work, in debug also, with the 4 imported static libs and using this fake linker for compiling:

Code: Select all

; -----------------------------------------------------------------------------
; Link.pb - Test only: remove libucrt.lib
; Get Compilation via a fake Link.exe:
; -----------------------------------------------------------------------------
; Compile it as Link.exe
; Then In PureBasic_6.10_x64\Compilers\vc, rename Link.exe to Link_real.exe and copy your Fake Link.exe in PureBasic_6.10_x64\Compilers\vc 
; Do not forget to restore Link_real.exe to Link.exe when you no longer need this hack
; -----------------------------------------------------------------------------
; To be used with with souces containing:
; Import "ucrt.lib"      : EndImport  ;; libucrt.lib       The Universal CRT (UCRT) contains the functions and globals exported by the standard C99 CRT library.
; Import "vcruntime.lib" : EndImport  ;; libvcruntime.lib  The vcruntime library contains Visual C++ CRT implementation-specific code: exception handling and debugging support, runtime checks and type information, implementation details, and certain extended library functions.
; Import "msvcrt.lib"    : EndImport  ;; libcmt.lib        This code handles CRT startup, internal per-thread data initialization, and termination.      
; Import "msvcprt.lib"   : EndImport  ;; libcpmt.lib       C++ standard library (STL) .lib files
; -----------------------------------------------------------------------------
EnableExplicit

Define CountParam, ProgramParam$, linkParam$, I 
Define Link, ExitCode
Define CompilerPath$ = GetPathPart(ProgramFilename())

If FileSize(CompilerPath$ + "link_real.exe") > 0
  CountParam = CountProgramParameters()
  For I=0 To CountParam-1
    linkParam$ + ProgramParameter(I) + " "
  Next
  
  ; Remove libucrt.lib, libvcruntime.lib and libcmt.lib and use the imported dynamic library instead: ucrt.lib, libvcruntime.lib, libcmt.lib  
  ; Remove libcpmt.lib from its place and add it just before msvcprt.lib. Don't know why order is important here.
  ; With msvcprt.lib only, there is a linker error: mismatch detected For 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in pbscintilla.LIB 
  If FindString(linkParam$, " ucrt.lib")
    linkParam$ = ReplaceString(linkParam$, " libucrt.lib", "")
  EndIf
  If FindString(linkParam$, " vcruntime.lib")
    linkParam$ = ReplaceString(linkParam$, " libvcruntime.lib", "")
  EndIf
  If FindString(linkParam$, " msvcrt.lib")
    linkParam$ = ReplaceString(linkParam$, " libcmt.lib", "")
  EndIf
  If FindString(linkParam$, " msvcprt.lib")
    linkParam$ = ReplaceString(linkParam$, " libcpmt.lib", "")
    linkParam$ = ReplaceString(linkParam$, " msvcprt.lib", " libcpmt.lib msvcprt.lib")
  EndIf
  
  Link = RunProgram(CompilerPath$ + "link_real.exe", linkParam$, GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Hide | #PB_Program_Connect | #PB_Program_Error)
  If WaitProgram(Link)
    ExitCode = ProgramExitCode(Link)
    If ExitCode
      ; gcc -O0 no optimization | -O2 generated code optimized to level 2 - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
      Define gccParam$ = "-O0 -g -fsigned-char -Wno-discarded-qualifiers -Wno-incompatible-pointer-types -Wno-overflow -Wno-int-conversion -c -o PureBasic.obj purebasic.c "
      ;Define gccParam$ = "-O2 -fno-tree-vrp -freorder-blocks-algorithm=simple -fno-schedule-insns -fno-schedule-insns2 -fno-strict-aliasing -fsigned-char -Wno-discarded-qualifiers -Wno-incompatible-pointer-types -Wno-overflow -Wno-int-conversion -c -o PureBasic.obj purebasic.c" 
      CompilerPath$ = Left(CompilerPath$, Len(CompilerPath$)- 3)
      SetClipboardText("Cd /D " + GetCurrentDirectory() +#CRLF$+ 
                       "PATH=%PATH%;" + CompilerPath$ + ";" + CompilerPath$ + "gcc\" + ";" + CompilerPath$ + "vc\" +#CRLF$+
                       "gcc.exe " + gccParam$ +#CRLF$+
                       "link_real.exe " + linkParam$ +#CRLF$+ "")
      MessageRequester("Compilation Error", "Link_real.exe exitcode = " + Str(ExitCode) +#CRLF$+#CRLF$+
                                            "Compilation cmdlines copied to the clipboard for testing." +#CRLF$+ 
                                            "To be used in cmd.exe before closing this window", #PB_MessageRequester_Ok | #PB_MessageRequester_Error)
    EndIf
    End ExitCode
  EndIf
  
Else
  MessageRequester("Compilation Error", "Visual studio linker, Link_real.exe, not found!", #PB_MessageRequester_Ok | #PB_MessageRequester_Error)
EndIf
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Visual Studio linker download

Post by chi »

ChrisR wrote: Tue Apr 30, 2024 10:18 am I have a mismatch error detected for...
ChrisR wrote: Tue Apr 30, 2024 10:18 am Don't know why but the order is important here!
There are probably more libs in PB with similar problems that we don't know about yet. That's why it's up to Fred to sort these things out before we have to fiddle around ;). He already mentioned an implementation to switch between static/dynamic linking.
Et cetera is my worst enemy
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Visual Studio linker download

Post by ChrisR »

Yes, you're right, I wanted to see the result for myself and to learn as well, but I'll be patient and let Fred implement it. It should close the exe's size debate with 6.10 and up.
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Visual Studio linker download

Post by Rinzwind »

Can this be documented somewhere? I wasn't aware that placing the VC folder in Compilers was an option. Only matters for asm compiler I guess?
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Visual Studio linker download

Post by fryquez »

Rinzwind wrote: Mon May 20, 2024 6:04 am Can this be documented somewhere? I wasn't aware that placing the VC folder in Compilers was an option. Only matters for asm compiler I guess?
There is no need for documentation, the linker will change with one of the next beta version anyway.
The Pelles linker is not suited anymore.
Post Reply