3

I’m trying to use v8 library in another Visual Studio C++ project. It is not my everyday environment so I may be missing something obvious.

I think I did everything as it is stated here:
https://code.google.com/p/v8/wiki/BuildingWithGYP

i.e.
checked out the following repos:

svn co http://src.chromium.org/svn/trunk/tools/third_party/python_26 third_party/python_26 --revision 89111  
svn co http://src.chromium.org/svn/trunk/deps/third_party/cygwin third_party/cygwin --revision 66844  
svn co https://src.chromium.org/chrome/trunk/deps/third_party/icu52 third_party/icu --revision 277999  
svn co http://googletest.googlecode.com/svn/trunk testing/gtest --revision 643  
svn co http://googlemock.googlecode.com/svn/trunk testing/gmock --revision 410  

Then from the root of the v8 project in the cmd window I ran:

python build\gyp_v8 -Dcomponent=static_library -Dtarget_arch=x64 -Dv8_enable_i18n_support=0 -Dv8_use_snapshot=0

…and got:

Updating projects from gyp files...  
Warning: Missing input files:  
C:\...\v8\tools\gyp\..\..\src\uri.h  
C:\...\v8\tools\gyp\..\..\src\feedback-slots.h  
Running build/landmines.py...  

Next I ran:

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.com" /build Release build\All.sln  

…and got:

(...)  
19>------ Build started: Project: All, Configuration: Release x64 ------  
========== Build: 18 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

From the errors I got I assume that the one that failed is the test project:

…18>C:\...\v8\testing\gtest\include\gtest/internal/
gtest-param-util.h(393): error C2868: 'testing::internal::TestMetaFactory<v8::in
ternal::compiler::MachineStoreOperatorTest_ParameterIsCorrect_Test>::ParamType'
: illegal syntax for using-declaration; expected qualified-name (compiler\machin
e-operator-unittest.cc)…

After this I created new C++ Empty project in Visual Studio. Added one file to the Source Files node with this sample content:

#include <include\v8.h>
#include <include\libplatform\libplatform.h>

using namespace v8;

Local<Array> NewPointArray(int x, int y, int z) {
   v8::Isolate* isolate = v8::Isolate::GetCurrent();

   // We will be creating temporary handles so we use a handle scope.
   EscapableHandleScope handle_scope(isolate);

   // Create a new empty array.
   Local<Array> array = Array::New(isolate, 3);

   // Return an empty result if there was an error creating the array.
   if (array.IsEmpty())
   return Local<Array>();

   // Fill out the values
   array->Set(0, Integer::New(isolate, x));
   array->Set(1, Integer::New(isolate, y));
   array->Set(2, Integer::New(isolate, z));

   // Return the value through Escape.
   return handle_scope.Escape(array);
   }

int main(){}

I added path to the root folder of the v8 project I checked out earlier in the C/C++ -> General -> Additional Include Directories property (not to the Visual Studio project since there are no v8 header files there).

I also added path to the build\Release\lib folder from the same v8 project in the Linker -> General -> Additional Library Directories property. This folder contains following:

gmock.lib  
gtest.lib  
v8_base.lib  
v8_libbase.lib  
v8_libplatform.lib  
v8_nosnapshot.lib  

What should I do next.

When I try to build the project in the Visual Studio I get:

1>------ Build started: Project: SOProject, Configuration: Debug Win32 ------
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall v8::HandleScope::~HandleScope(void)" (??1HandleScope@v8@@QAE@XZ) referenced in function "public: __thiscall v8::EscapableHandleScope::~EscapableHandleScope(void)" (??1EscapableHandleScope@v8@@QAE@XZ)
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall v8::EscapableHandleScope::EscapableHandleScope(class v8::Isolate *)" (??0EscapableHandleScope@v8@@QAE@PAVIsolate@1@@Z) referenced in function "class v8::Local<class v8::Array> __cdecl NewPointArray(int,int,int)" (?NewPointArray@@YA?AV?$Local@VArray@v8@@@v8@@HHH@Z)
1>Source.obj : error LNK2019: unresolved external symbol "private: class v8::internal::Object * * __thiscall v8::EscapableHandleScope::Escape(class v8::internal::Object * *)" (?Escape@EscapableHandleScope@v8@@AAEPAPAVObject@internal@2@PAPAV342@@Z) referenced in function "public: class v8::Local<class v8::Array> __thiscall v8::EscapableHandleScope::Escape<class v8::Array>(class v8::Local<class v8::Array>)" (??$Escape@VArray@v8@@@EscapableHandleScope@v8@@QAE?AV?$Local@VArray@v8@@@1@V21@@Z)
1>Source.obj : error LNK2019: unresolved external symbol "public: static class v8::Local<class v8::Integer> __cdecl v8::Integer::New(class v8::Isolate *,int)" (?New@Integer@v8@@SA?AV?$Local@VInteger@v8@@@2@PAVIsolate@2@H@Z) referenced in function "class v8::Local<class v8::Array> __cdecl NewPointArray(int,int,int)" (?NewPointArray@@YA?AV?$Local@VArray@v8@@@v8@@HHH@Z)
1>Source.obj : error LNK2019: unresolved external symbol "public: bool __thiscall v8::Object::Set(unsigned int,class v8::Handle<class v8::Value>)" (?Set@Object@v8@@QAE_NIV?$Handle@VValue@v8@@@2@@Z) referenced in function "class v8::Local<class v8::Array> __cdecl NewPointArray(int,int,int)" (?NewPointArray@@YA?AV?$Local@VArray@v8@@@v8@@HHH@Z)
1>Source.obj : error LNK2019: unresolved external symbol "public: static class v8::Local<class v8::Array> __cdecl v8::Array::New(class v8::Isolate *,int)" (?New@Array@v8@@SA?AV?$Local@VArray@v8@@@2@PAVIsolate@2@H@Z) referenced in function "class v8::Local<class v8::Array> __cdecl NewPointArray(int,int,int)" (?NewPointArray@@YA?AV?$Local@VArray@v8@@@v8@@HHH@Z)
1>Source.obj : error LNK2019: unresolved external symbol "public: static class v8::Isolate * __cdecl v8::Isolate::GetCurrent(void)" (?GetCurrent@Isolate@v8@@SAPAV12@XZ) referenced in function "class v8::Local<class v8::Array> __cdecl NewPointArray(int,int,int)" (?NewPointArray@@YA?AV?$Local@VArray@v8@@@v8@@HHH@Z)
1>D:\PROGRAMMING\!VisualStudioProjects\SOProject\Debug\SOProject.exe : fatal error LNK1120: 7 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How should I read this? From other posts like:

How to compile the smallest possible V8 library for Windows?
https://groups.google.com/forum/#!topic/v8-users/KhniGgixxGM
Embedding v8 with Visual C++ 2010

…I got that I need to link another libraries.

Which ones exactly, why these and how do I link them in Visual Studio exactly. Of course I tried different combinations and solutions but without any positive result.

Thanks in advance.

Community
  • 1
  • 1
listerreg
  • 584
  • 1
  • 6
  • 15
  • Target platform, debug level and code generation options for your project should match those specified in libraries, i.e. it is not possible to link "Release x64" libraries with "Debug Win32" binary, either build debug versions of x86 libraries or configure x64 release build for executable. Also check "C/C++ -> Code Generation -> Runtime Library" option in your project and libraries it must also match (it is Multihreaded DLL for release build by default and maybe should be changed to Multihreaded) – dewaffled Oct 26 '14 at 21:21

2 Answers2

4

I'm a student at a school where we make a video game as a team over the course of two semesters for one of our classes. We're working on getting V8 integrated at the moment to provide scripting capabilities for our game engine. We've hit a bunch of snags in integrating V8 into the engine itself, and, depending on when we get this stuff fully solved, I'll end up writing a massive blog post either over winter break or summer break. I'll fully detail exactly how to get V8 up and running in a Windows application, and more specifically for a video game.

But in the meantime, I can tell you at least how we got V8 to build for Windows, and how we make it accessible by our game project.

  1. Install Python 2.x, and make sure it is added to your PATH. If you didn't know, you can edit your PATH right-clicking and selecting Properties of My Computer/This PC. Then, you can check the PATH by clicking "Change Settings", going to the "Advanced" tab, and clicking "Environment Variables". Edit the "Path" variable, and make sure C:\Python27 or whatever is in there. Semicolons separate PATH entries. Also, make sure that if you have Python 3.x installed, that it is NOT in your PATH. You can always put it back later if it is and you need it, but remove it for now if it does exist.

  2. Install SVN, and make sure it is added to your path. I super hate SVN, so I was unwilling to install it natively for Windows, and instead just installed it as part of my cygwin bundle. Then I put C:\cygwin64\bin in my PATH.

  3. Figure out where you're going to have all this work get done, and make a new file called v8.cmd there. (cmd files are Windows batch files, if you didn't know.) For me, I just put it in the root of my secondary hard drive, D:. So I had D:\v8.cmd.

  4. Edit v8.cmd, put the following as the contents of the file, save and quit:

    svn checkout http://v8.googlecode.com/svn/tags/3.28.57/ v8-build
    svn checkout http://src.chromium.org/svn/trunk/deps/third_party/cygwin@66844 v8-build/third_party/cygwin
    svn checkout http://gyp.googlecode.com/svn/trunk@1831 v8-build/gyp
    
    mkdir v8
    mkdir v8\lib
    mkdir v8\lib\Release
    mkdir v8\lib\Debug
    mkdir v8\include
    
    call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"
    
    set CYGWIN_ROOT=%cd%\v8-build\third_party\cygwin
    set PATH=%CYGWIN_ROOT%\bin;%PATH%
    set PYTHONPATH=%cd%\v8-build\gyp\pylib
    
    call v8-build\third_party\cygwin\setup_mount.bat
    call v8-build\third_party\cygwin\setup_env.bat
    
    python v8-build\build\gyp_v8 -Dcomponent=static_library -Dtarget_arch=ia32 -Dv8_enable_i18n_support=0 -Dv8_use_snapshot=0
    
    msbuild /m /p:UseEnv=true /p:Configuration=Release /p:Platform=x86 v8-build\tools\gyp\v8.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Release /p:Platform=x86 v8-build\tools\gyp\v8_libplatform.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Release /p:Platform=x86 v8-build\tools\gyp\v8_libbase.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-build\tools\gyp\v8.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-build\tools\gyp\v8_libplatform.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-build\tools\gyp\v8_libbase.vcxproj
    
    xcopy /y /c "v8-build\build\Debug\lib\*.lib" "v8\lib\Debug\"
    xcopy /y /c "v8-build\build\Release\lib\*.lib" "v8\lib\Release\"
    xcopy /y /c /s "v8-build\include\*.h" "v8\include\"
    pause
    
  5. Start an Administrator Command Prompt. In Windows 8 (and maybe 7?), you can do this by right-clicking on the Start Button (or the lower-left corner of the screen) and clicking Command Prompt (admin).

  6. Navigate to the location of v8.cmd, and run it by typing "v8.cmd". This does several things:

    • Create a "v8-build" folder, and check out the necessary things from SVN to build the library (v8, the cygwin v8 needs for gyp, and gyp).

    • Create a "v8" folder, which is where the final stuff for your project will be built.

    • Set the environment variables necessary to easily run the Visual Studio build system from this script. This is assuming you're using Visual Studio 2013.

    • Set the environment variables necessary to use the V8 "versions" of cygwin and Python.

    • Call some batch files that do some more environment variable stuff, I don't know, they come with V8.

    • Run gyp using what is now V8's version of Python. This will generate the Visual Studio project files that, when built, will give you the necessary libraries. (AS A NOTE: This whole thing is assuming you want to statically link V8 to your project, and do NOT want to use DLLs.)

    • Now it'd be cool if we could just build a Visual Studio solution, because that's what solutions are for, building multiple projects at a time, but gyp is a piece of crap and the solutions it builds don't work. Thus, we have to build each project that we want individually. This is why there are six separate "msbuild" lines in the batch file.

    • Once the libraries are built, copy the output lib files, as well as the necessary header files, to the v8/ folder.

    • And we're done!

  7. Okay, so now everything we need to include in our project is in the v8/ folder. Now, a quick aside about how my team has our project set up. Our solution looks like this:

    [+] OmniArch/                        Solution root folder
     |
     +--[+] include/                     Folder for all external library .h files
     |   |
     |   +-- v8.h
     |   +-- v8config.h
     |   +-- v8-debug.h
     |   +-- v8-platform.h
     |   +-- v8-preparser.h
     |   +-- v8-profiler.h
     |   +-- v8stdint.h
     |   +-- v8-testing.h
     |   +-- v8-util.h
     |
     +--[+] lib/                         Folder for all external library .lib files
     |   |
     |   +--[+] Debug/
     |   |   |
     |   |   +-- v8_base.lib
     |   |   +-- v8_libbase.lib
     |   |   +-- v8_libplatform.lib
     |   |   +-- v8_nosnapshot.lib
     |   |
     |   +--[+] Release/
     |       |
     |       +-- v8_base.lib
     |       +-- v8_libbase.lib
     |       +-- v8_libplatform.lib
     |       +-- v8_nosnapshot.lib
     |
     +--[+] OmniArch/                    Folder for the "OmniArch" project
     |
     +--[+] OmniEngine/                  Folder for the "OmniEngine" project
     |
     +-- OmniArch.sln                    The solution file
    

    The rest of this guide is going to explain how to include V8, assuming your folder structure looks like this. If you don't want yours to look like this, modify stuff accordingly. Anyways, copy the contents of the v8/ folder into your solution root folder. If you didn't have a lib/ and include/ folder before, now you do, and if you did, now they have more files in them.

  8. In Visual Studio, right-click on your project (the project, not the solution), and click Properties. Make sure the box at the top is set to "All Configuration", and not "Debug" or "Release". Under "Configuration Properties", select "VC++ Directories".

    • At the end of the "Library Directories" line, put a semicolon, followed by

      $(SolutionDir)lib;$(SolutionDir)lib\$(Configuration)
      
    • At the end of "Include Directories", put a semicolon, followed by

      $(SolutionDir)include
      
  9. Still in the properties page, under "Configuration Properties" -> "Linker" -> "Input" -> "Additional Dependencies" (or "Configuration Properties" -> "Librarian" -> "General" -> "Additional Dependencies"), add the following, using a semicolon if needed:

    v8.lib;v8_base.lib;v8_libbase.lib;v8_nosnapshot.lib;v8_libplatform.lib;WS2_32.lib;Winmm.lib
    

If your configuration options are different for Debug and Release, then change to each of those and add the above options, instead of overriding both of them.

  1. Now you should be totally ready to build. You should be able to

    #include "v8.h"
    

and have it work. And you should be able to build without error.

Let me know if this helped at all, and if you run into any problems. I spent like four days just banging my head against the wall to make this work.

Adam Rezich
  • 3,122
  • 6
  • 31
  • 39
  • Why disable snapshots? Shouldn't the snapshot feature improve performance? – Boinst Jan 21 '15 at 04:19
  • I am trying to build x64. I've replaced obvious parts in your batch, but building every .vcxproj with MSBuild gives me errors like "fatal error C1189: #error : Target architecture x64 is only supported on x64 host". Building .sln for x64 succeeds, but i can't find v8_libbase.lib in output... – Rast Jan 23 '15 at 14:18
  • Is v8.lib not required? I have all these libs refenced, and externals such as `__declspec(dllimport) public: bool __thiscall v8::Value::IsStringObject(void)const " (__imp_?IsStringObject@Value@v8@@QBE_NXZ)` cannot be resolved. – James Wilkins Feb 03 '15 at 17:09
0

When you set "Linker -> General -> Additional Library Directories" property, you only added a path where linker should search V8 libraries.

Append these V8 libraries into "Linker -> Input -> Additional Dependencies" property:

v8_base.lib;v8_libbase.lib;v8_libplatform.lib;v8_nosnapshot.lib
pmed
  • 1,536
  • 8
  • 13