lld-link: error: <root>: undefined symbol: mainCRTStartup when building V8 - visual-studio-2017

I've spent a whole day on this now and I can't seem to get the .lib files to build with VS 2017. I followed the V8 docs here:
https://v8.dev/docs/build
Following the instructions does work, but I end up with a V8 command line program in the out directory and .lib that do NOT work with Visual Studio 2017.
fatal error LNK1107: invalid or corrupt file: cannot read at 0x1422A
I executed this to try to get the build files for the libraries only:
gn gen out/lib --args="v8_static_library=true v8_use_snapshot=true v8_use_external_startup_data=false v8_monolithic=true icu_use_data_file=false is_component_build=false is_debug=false"
Then ran this: ninja -C out/lib
And this was the final result:
ninja: Entering directory `out/lib'
[1632/1645] LINK cctest.exe cctest.exe.pdb
FAILED: cctest.exe cctest.exe.pdb
ninja -t msvc -e environment.x64 -- ../../../../third_party/llvm-build/Release+Asserts/bin/lld-link.exe /nologo /OUT:./cctest.exe /PDB:./cctest.exe.pdb #./cctest.exe.rsp
lld-link: error: <root>: undefined symbol: mainCRTStartup
[1634/1645] LINK generate-bytecode-expectations.exe generate-bytecode-expectations.exe.pdb
ninja: build stopped: subcommand failed.
I think I'm missing something, but I have no idea at the moment.

Ok, seems my first mistake was changing the prompt to v8\tools\dev\ and working from there. The "normal" steps I found actually only work properly from the root of the source. I ended up with v8\tools\dev\out\x64.release then ninja -C out/x64.release v8 failed because v8 was not accepted in this setup for some reason.
The other thing I did wrong was to edit the args.gn file directly and save it. The CORRECT process is to run gn args out.gn\x64.release so that after you save AND close the editor, it automatically re-generates/updates the files. Most likely changing the file has no effect on its own, and you'll get confused because ninja won't even see the changes.
The linker error about corrupt files is because is_clang is true by default. Setting is_clang=false fixes that error. It just works, I have no idea why; just take it and go ... ;)
The correct way, from the root that worked for me is:
python tools\dev\v8gen.py x64.release
python tools\dev\v8gen.py ia32.release
python tools\dev\v8gen.py x64.debug
python tools\dev\v8gen.py ia32.debug
This will output files that can be compiled in the "v8\out.gn" folder.
Tip: Run "python tools\dev\v8gen.py list" to see a list of possible build configurations.
Next I updated the build arguments:
gn args out.gn\x64.release
Using these:
is_debug = false <-(or true for debug builds)
target_cpu = "x64"
is_component_build = false
v8_static_library = true
use_custom_libcxx = false
use_custom_libcxx_for_host = false
v8_use_external_startup_data = false <-(or true to use the bin startup files)
is_clang = false
And again for the 32-bit version (changing "x64" above to "x86" of course):
gn args out.gn\ia32.release
Then repeated all the above for x64.debug and ia32.debug.
And compiled them:
ninja -C out.gn/x64.debug v8
ninja -C out.gn/ia32.debug v8
ninja -C out.gn/x64.release v8
ninja -C out.gn/ia32.release v8
Visual Studio 2017 now builds and links my project against them again now (was an old project I resurrected).
Note: Linking against debug versions of V8 libraries may give an error that _ITERATOR_DEBUG_LEVEL does not match. To fix this I simply went to the C++ project settings (Confiuration Properties->C/C++->Preprocessor->Preprocesor Definitions) and added ;_ITERATOR_DEBUG_LEVEL=0 so it matches.

Related

VS Code building .exe files in Windows 10, how do I change to build for windows 10 compatibility

So I am having trouble building my .exe files in visual studio code for my current windows OS. for some reason, my .exe files, when I run a diagnostic on them, seem to only be compatible for windows 8, not 10.
Using
Processor architecture: AMDx64
system: x64 based PC
VS code version: 1.74.3
When creating a simple "Hello World" application I tried following this tutorial, and it didn't have any problems. It was when I followed the tutorial for importing external libraries that the problems ocured.
I’ve tried importing an external library, and used msys2 to install the files in the bin/include/lib folders for mingw64.
I set my include path to the include folder, and I’ve set my compiler to default. My JSON tasks document appears correct, and when I build the .exe file, it builds successfully… but it only builds an executable file compatible for windows eight
I ran the properties compatibility test, and this is the output I get
What exactly do I need to do in order to change the OS version to make it compatible to run on both the visual studio code terminal, and my system terminal as well?
the following is the output once I ran the build process
Starting build...
C:\msys64\mingw64\bin\cpp.exe -IC:\msys64\mingw64\include -fdiagnostics-color=always -g "D:\Documents\C++\VS_Code\FMT Import\FMTImport.cpp" -o "D:\Documents\C++\VS_Code\FMT Import\FMTImport.exe" -lfmt
>Build finished successfully.
edit:
config name:C:/msys64/mingw64/bin/g++.exe
compiler path: C:/msys64/mingw64/bin/g++.exe
intellisense mode: ${default}
edit 2:
running terminal attempts to build..
for g++
D:\Documents\C++\VS_Code\FMT Import>g++ -o FMTImport FMTImport.cpp
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\William\AppData\Local\Temp\ccshGhdE.o:FMTImport.cpp:(.text+0x8c): undefined reference to `fmt::v9::vprint(fmt::v9::basic_string_view<char>, fmt::v9::basic_format_args<fmt::v9::basic_format_context<fmt::v9::appender, char> >)'
collect2.exe: error: ld returned 1 exit status
for clang++
D:\Documents\C++\VS_Code\FMT Import>clang++ FMTImport.cpp -o FMTImport
FMTImport.cpp:1:10: fatal error: 'fmt/format.h' file not found
#include <fmt/format.h>

How to Build Skia on Windows

I have not yet been able to successfully build skia from source code on Windows 10. I am going in circles at this point.
I followed the official instructions here https://skia.org/user/build but always get errors.
gn args are:
is_official_build=true
is_component_build = false
win_vc="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC"
skia_use_libpng=false
skia_use_icu=false
skia_use_zlib=false
skia_use_libwebp=false
skia_enable_pdf=false
skia_use_libjpeg_turbo=false
skia_use_expat=false
I get the following error:
C:\Development\skia\bin>ninja -C out/Static
ninja: Entering directory `out/Static'
[13/2662] copy ../../../third_party/externals/icu/common/icudtl.dat icudtl.dat
FAILED: icudtl.dat
python C:/Development/skia/gn/cp.py ../../../third_party/externals/icu/common/icudtl.dat icudtl.dat
CreateProcess failed: The system cannot find the file specified.
ninja: fatal: ReadFile: The handle is invalid.
Any thoughts would be appreciated.
By the way, first attempt at:
using gn or ninja to build anything
using Chromium's depot_tools
So, I am not ruling out any user error on my part :(

Trying to build mysql-connector-cpp on Windows

I want to be able to build mysql-connector/c++ applications in debug mode (VS2k17), and understand the only way I can do that is to first build mysql-connector-cpp from source on my computer (Windows 10/64bit). I tried the following:
cmake . -DWITH_SSL=C:\Users\Rawb\source\libraries\OpenSSL-Win64
and it looks to complete successfully ...
Install location: C:/Users/Rawb/MySQL/MySQL Connector C++ 8.0
Connector libraries will be installed at: lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Rawb/source/repos/mcc2/mysql-connector-cpp
but when I run the following cmake build ...
cmake --build . --config
it fails with this output ...
LINK : fatal error LNK1104: cannot open file 'ssleay32.lib' [C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\connector.vcxproj]
Done Building Project "C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\connector.vcxproj" (default targets) -- FAILED.
Done Building Project "C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\ALL_BUILD.vcxproj" (default targets) -- FAILED.
Build FAILED.
"C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\ALL_BUILD.vcxproj" (default target) (1) ->
"C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\connector.vcxproj" (default target) (10) ->
(Link target) ->
LINK : fatal error LNK1104: cannot open file 'ssleay32.lib' [C:\Users\Rawb\source\repos\mcc2\mysql-connector-cpp\connector.vcxproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:14.57
What might I be doing wrong? Do I just need to place the ssleay32.lib file somewhere? I do have a copy of it but not sure how it should be included. Or is it more complex? I.e. Do I need to build Open SSL first on my machine as well? This is a deep rabbit hole for me, I may be in over my head haha!
NOTE: I also asked this question on reddit with no luck, so hoping someone here might know.
If you're using MSVS (any version, including MSVS 2017), then I'd suggest using "NMake" (instead of cmake). Or just create a new C/C++ library project in the MSVS IDE.
The flag you're looking for is /LIBPATH. I don't think you need "-D", but you certainly need "/LIBPATH".
Here are the MSVS linker options:
https://learn.microsoft.com/en-us/cpp/build/reference/linker-options?view=vs-2017
Finally, you might be able to do all the troubleshooting/tracing you want without actually debugging (and rebuilding) the connector code.
Here's how to enable tracing in the MySQL Connector:
https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-debug-tracing.html
Please let me know if this helps.

LNK1181 error when compiling V8 engine on Win10

I'm following this guide on building V8 but I am hitting some issues on the compilation step. I am running Windows 10 x64. I am trying to compile with options to embed the engine also.
Running the following command:
ninja -C out.gn/x64.release
Gives me this error:
ninja: Entering directory `out.gn/x64.release'
[1/471] LINK mksnapshot.exe mksnapshot.exe.pdb
FAILED: mksnapshot.exe mksnapshot.exe.pdb
C:/Workspace/depot_tools/win_tools-2_7_6_bin/python/bin/python.exe ../../build/toolchain/win/tool_wrapper.py link-wrapper environment.x64 False link.exe /nologo /OUT:./mksnapshot.exe /PDB:./mksnapshot.exe.pdb #./mksnapshot.exe.rsp
LINK : fatal error LNK1181: cannot open input file 'comdlg32.lib'
ninja: build stopped: subcommand failed.
Now I believe I have narrowed down the error to looking for the .lib files in the wrong directory. I have (had) multiple versions installed, so there were multiple folders in my Windows Kit install.
Windows Kits/10/Lib/10.0.16299.0
Windows Kits/10/Lib/10.0.15xxx.0
If I dragged and dropped the comdlg32.lib file from 10.0.16299.0 into the 10.0.15xxx.0 directory then the error changed to a LNK1181 error with a different input file. I did this a few times but I was unsure if this was going to cause issues with different versions and there was probably going to be a lot.
I uninstalled the 10.0.15xxx.0 version which left behind the folder I mentioned, so I removed that and after doing so I have started getting the LNK1181 error with a different input file (advapi32.lib I assume the very first file it can't find). This is how I came to the conclusion about the path being incorrect.
So I have tried a few things to change the path (I hoped just uninstalling the old version would fix it) such as:
Uninstalling the old version.
Going through registry entries to see if I can find an install path or something using that path, which I didn't. I did notice that there was still installation and data in the registry for the 10.0.15xxx.0 install, I might try deleting that from the registry directly as a last resort?
I have tried to explicitly set the path by setting <TargetUniversalCRTVersion>10.0.16299.0</TargetUniversalCRTVersion> in this file: C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\uCRT.props
I have never used Ninja before so I tried looking for a way to set some kind of lib-path in the command but couldn't really find anything.
I looked through the python scripts being executed to try and locate something to do with the libs path but couldn't see anything.
I would be grateful for any help and suggestions. Thanks.
You can try to compile v8 using Visual Studio as explained here: https://chromium.googlesource.com/chromium/src/+/master/docs/windows_build_instructions.md#using-the-visual-studio-ide
By running the following commands:
$ gn gen --ide=vs out.gn/x64.release
$ cd out.gn/x64.release
$ msbuild all.sln
You can see a full example here: https://github.com/phpv8/v8js/issues/272#issuecomment-262848754
Apparently this method is not officially supported anymore, but I had the same problem as you have and this solved the issue for me.
Note that after this I had another issue, the unit tests failed to be compiled due to a linking error, but I had the necessary libraries to use v8. So there may be deeper problem that is causing all of this that I'm missing.
Edit:
Also, you could try to set the following parameters with gn args:
visual_studio_path = "..."
visual_studio_version = "2017"
wdk_path = "..."
windows_sdk_path = "C:\Program Files (x86)\Windows Kits\10"
To set those parameters, do:
gn args out.gn/x64.release
This will open a text editor where you can write the extra parameters you are interested in.
To see the full list of parameters you can specify:
gn args --list out.gn/x64.release
I was following this guide https://medium.com/dailyjs/how-to-build-v8-on-windows-and-not-go-mad-6347c69aacd4 and also ran into the error
LINK1181: cannot open input file 'advapi32.lib'
I'm pretty sure it was because I had the wrong versions of the Windows 10 SDK. Similar to you I had versions:
Windows Kits/10/Lib/10.0.10240.0
Windows Kits/10/Lib/10.0.16299.0
But according to https://chromium.googlesource.com/chromium/src/+/master/docs/windows_build_instructions.md#Setting-up-Windows (Which I think is relevant) you need version 10.0.15063.0
After installing version 10.0.15063.0 (with the visual studio installer) to
Windows Kits/10/Lib/10.0.15063.0
I was able to continue with the build.

fatal error U1073: don't know how to make 'c:\winddk\7600.16385.0\lib\wxp\i386\msvcrt_winxp.obj'

I try to compile a driver for WinXP x86 Release, using these commands:
C:\WinDDK\7600.16385.0\bin\setenv.bat C:\WinDDK\7600.16385.0\ fre x86 WXP no_oacr
cd C:\src
build
It fails because of these U1073 errors about msvcrt_winxp.obj. I checked and there is no msvcrt_winxp.obj file anywhere in C:\WinDDK or its subdirectories.
It looks to me the problem is not in the actual code, but maybe I haven't set up the DDK right before compiling. Why do I get these U1073 errors?
I found this similar question - driver build is failing for amd64 via winddk, but there the OP did has not included relevant information such as his build output, and his question is unanswered. I also found this thread - http://www.techtalkz.com/microsoft-device-drivers/295015-wdk-linker-error-u1073.html, but there is no answer there, as well.
My Build Output:
BUILD: Compile and Link for x86
BUILD: Loading c:\winddk\7600.16385.0\build.dat...
BUILD: Computing Include file dependencies:
BUILD: Start time: Thu Nov 13 12:04:25 2014
BUILD: Examining c:\src directory tree for files to compile.
c:\src
c:\src\common
c:\src\driver
c:\src\ui
BUILD: Saving c:\winddk\7600.16385.0\build.dat...
1>BUILD: Compiling (NoSync) c:\src\driver directory
2>BUILD: Compiling (NoSync) c:\src\ui directory
_NT_TARGET_VERSION SET TO WINXP
_NT_TARGET_VERSION SET TO WINXP
1>BUILD: Linking for c:\src\driver directory
2>BUILD: Linking for c:\src\ui directory
_NT_TARGET_VERSION SET TO WINXP
_NT_TARGET_VERSION SET TO WINXP
1>errors in directory c:\src\driver
1>NMAKE : fatal error U1073: don't know how to make 'c:\winddk\7600.16385.0\lib\wxp\i386\msvcrt_winxp.obj'
2>errors in directory c:\src\ui
2>NMAKE : fatal error U1073: don't know how to make 'c:\winddk\7600.16385.0\lib\wxp\i386\msvcrt_winxp.obj'
1>nmake.exe /nologo BUILDMSG=Stop. -i /nologo /f c:\winddk\7600.16385.0\bin\makefile.def BUILD_PASS=PASS2 LINKONLY=1 NOPASS0=1 MAKEDLL=1 MAKEDIR_RELATIVE_TO_BASEDIR= failed - rc = 2
2>nmake.exe /nologo BUILDMSG=Stop. -i /nologo /f c:\winddk\7600.16385.0\bin\makefile.def BUILD_PASS=PASS2 LINKONLY=1 NOPASS0=1 MAKEDLL=1 MAKEDIR_RELATIVE_TO_BASEDIR= failed - rc = 2
BUILD: Finish time: Thu Nov 13 12:04:26 2014
BUILD: Done
0 files compiled - 4 Errors
I don't know why, but after I installed an even newer version - 7600.16385.1 from http://www.microsoft.com/en-us/download/details.aspx?id=11800, building worked fine, without any errors.
It didn't work the first time, however, but I noticed it was still complaining about 7600.16385.0, so I removed any intermediary build output, .err and .log files. After compiling again, it worked this time.
I also noticed that msvcrt_winxp.obj was present in c:\WinDDK\7600.16385.1, but not in c:\WinDDK\7600.16385.0.
I must admit that I hadn't downloaded c:\WinDDK\7600.16385.0, it was an old folder I found on my hard-drive, and I assumed I had used it to compile the project before, so I just copied it. The problems might have been because of this.
So in conclusion, the problem was with my WinDDK install, not with the project itself, and installing a newer version fixed it. Maybe re-installing the same version would have worked, too, but I have no need to verify that now that's working with the newer version.