如何在Visual Studio c++项目中链接使用google v8所需的库

How to link required libraries to use google v8 in Visual Studio C++ project

本文关键字:v8 google 链接 Visual Studio c++ 项目      更新时间:2023-10-16

我正试图在另一个Visual Studio c++项目中使用v8库。这不是我的日常环境,所以我可能错过了一些明显的东西。


https://code.google.com/p/v8/wiki/BuildingWithGYP


查看了下面的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  

然后在命令行窗口中从v8项目的根目录运行:

python buildgyp_v8 -Dcomponent=static_library -Dtarget_arch=x64 -Dv8_enable_i18n_support=0 -Dv8_use_snapshot=0

,有:

Updating projects from gyp files...  
Warning: Missing input files:  
C:...v8toolsgyp....srcuri.h  
C:...v8toolsgyp....srcfeedback-slots.h  
Running build/landmines.py...  

接着我跑了:

"C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDEdevenv.com" /build Release buildAll.sln  

,有:

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

从我得到的错误中,我假设失败的是测试项目:

…18>C:...v8testinggtestincludegtest/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 (compilermachin
e-operator-unittest.cc)…

之后,我在Visual Studio中创建了一个新的c++ Empty项目。向Source Files节点添加了一个文件,其中包含以下示例内容:

#include <includev8.h>
#include <includelibplatformlibplatform.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(){}

我添加了路径到v8项目的根文件夹,我之前在C/c++ ->通用->附加包含目录属性(不是Visual Studio项目,因为那里没有v8头文件)。

我还在链接器->通用->附加库目录属性中从相同的v8项目中添加了buildReleaselib文件夹的路径。此文件夹包含以下内容:

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

接下来我该怎么做呢?

当我尝试在Visual Studio中构建项目时,我得到:

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!VisualStudioProjectsSOProjectDebugSOProject.exe : fatal error LNK1120: 7 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我应该怎么读这个?从其他帖子,如:

如何为Windows编译尽可能小的V8库?
https://groups.google.com/forum/#!主题/v8-users/KhniGgixxGM
使用Visual c++ 2010嵌入v8

…我知道我需要链接其他库。

到底是哪些,为什么是这些,以及如何在Visual Studio中准确地链接它们。当然,我尝试了不同的组合和解决方案,但没有任何积极的结果。

提前感谢。

我是一所学校的学生,我们用两个学期的时间为一个班级制作了一款电子游戏。我们现在正致力于整合V8,为我们的游戏引擎提供脚本功能。我们在将V8集成到引擎本身的过程中遇到了一些障碍,而且,取决于我们什么时候完全解决了这些问题,我将在寒假或暑假期间写一篇大型博客文章。我将详细说明如何在Windows应用程序中启动和运行V8,更具体地说,是在视频游戏中。

但与此同时,我至少可以告诉你我们是如何为Windows构建V8的,以及我们是如何在我们的游戏项目中使用它的。

  1. 安装Python 2。x,并确保将其添加到PATH中。如果你不知道,你可以编辑你的PATH,右键单击并选择我的电脑/这台电脑的属性。然后,您可以通过点击"更改设置",进入"高级"选项卡,点击"环境变量"来检查PATH。编辑"Path"变量,并确保C:Python27或任何在那里。分号分隔PATH条目。另外,如果你有Python 3,请确保。x安装后,它不在您的路径中。如果你需要它,你可以随时把它放回去,但如果它确实存在,现在就把它删除。

  2. 安装SVN,并确保将其添加到您的路径中。我非常讨厌SVN,所以我不愿意在Windows上本地安装它,而是将它作为cygwin包的一部分安装。然后我把C:cygwin64bin放到我的PATH中。

  3. 找出你将在哪里完成所有这些工作,并创建一个名为v8的新文件。cmd。(cmd文件是Windows批处理文件,如果你不知道的话。)对我来说,我只是把它放在我的二级硬盘D:的根目录下。所以我有D:v8.cmd

  4. 编辑v8。Cmd,输入如下内容,保存并退出:

    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 v8lib
    mkdir v8libRelease
    mkdir v8libDebug
    mkdir v8include
    call "C:Program Files (x86)Microsoft Visual Studio 12.0Common7ToolsVsDevCmd.bat"
    set CYGWIN_ROOT=%cd%v8-buildthird_partycygwin
    set PATH=%CYGWIN_ROOT%bin;%PATH%
    set PYTHONPATH=%cd%v8-buildgyppylib
    call v8-buildthird_partycygwinsetup_mount.bat
    call v8-buildthird_partycygwinsetup_env.bat
    python v8-buildbuildgyp_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-buildtoolsgypv8.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Release /p:Platform=x86 v8-buildtoolsgypv8_libplatform.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Release /p:Platform=x86 v8-buildtoolsgypv8_libbase.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-buildtoolsgypv8.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-buildtoolsgypv8_libplatform.vcxproj
    msbuild /m /p:UseEnv=true /p:Configuration=Debug /p:Platform=x86 v8-buildtoolsgypv8_libbase.vcxproj
    xcopy /y /c "v8-buildbuildDebuglib*.lib" "v8libDebug"
    xcopy /y /c "v8-buildbuildReleaselib*.lib" "v8libRelease"
    xcopy /y /c /s "v8-buildinclude*.h" "v8include"
    pause
    
  5. 启动管理员命令提示符。在Windows 8(也许还有7?)中,你可以通过右键单击开始按钮(或屏幕左下角)并单击命令提示符(admin)来做到这一点。

  6. 导航到v8的位置。Cmd,然后输入"v8.cmd"运行它。这可以做几件事:

    • 创建一个"v8-build"文件夹,并从SVN中签出构建库所需的东西(v8, cygwin v8需要gyp和gyp)。

    • 创建一个"v8"文件夹,这是你的项目最终的东西将被构建。

    • 设置必要的环境变量,以便从该脚本轻松运行Visual Studio构建系统。假设你使用的是Visual Studio 2013。

    • 设置使用V8版本cygwin和Python所需的环境变量

    • 调用一些批处理文件,做一些更多的环境变量的东西,我不知道,他们是V8的。

    • 使用现在的V8版本的Python运行gyp。这将生成Visual Studio项目文件,这些文件在构建时将为您提供必要的库。(注:这一切都是假设你想静态链接V8到你的项目,并不想使用dll)

    • 现在如果我们能构建一个Visual Studio解决方案就好了,因为这就是解决方案的目的,一次构建多个项目,但是gyp是一堆垃圾,它构建的解决方案不起作用。因此,我们必须单独构建我们想要的每个项目。这就是为什么批处理文件中有六个单独的"msbuild"行。

    • 一旦库被构建,复制输出库文件,以及必要的头文件,到v8/文件夹。

    • 我们完成了!

  7. 好了,现在我们需要在项目中包含的所有内容都在v8/文件夹中。现在,简单说一下我的团队是如何建立项目的。我们的解决方案是这样的:

    [+] 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
    

    本指南的其余部分将解释如何包含V8,假设您的文件夹结构如下所示。如果你不希望你的看起来像这样,修改相应的东西。无论如何,将v8/文件夹的内容复制到解决方案根文件夹中。如果你以前没有lib/include/文件夹,现在你有了,如果你有,现在他们有更多的文件。

  8. 在Visual Studio中,右键单击项目(项目,而不是解决方案),然后单击属性。确保顶部的方框设置为"All Configuration",而不是"Debug"或"Release"。在"配置属性"下,选择"vc++目录"。

    • 在"Library Directories"行末尾,加一个分号,后面跟着

      $(SolutionDir)lib;$(SolutionDir)lib$(Configuration)
      
    • 在"Include Directories"后面加一个分号,后面跟着

      $(SolutionDir)include
      
  9. 仍然在属性页面中,在"配置属性"->"链接器"->"输入"->"附加依赖项"(或"配置属性"->"图书管理员"->"通用"->"附加依赖项")下,添加以下内容,如果需要使用分号:

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

如果调试和发布的配置选项不同,则更改为每个选项并添加上述选项,而不是覆盖它们。

  1. 现在你应该完全准备好构建。你应该能够

    #include "v8.h"
    

让它工作。你应该能够构建没有错误。

让我知道这是否有帮助,如果你遇到任何问题。我花了差不多四天的时间,绞尽脑汁才让这部电影成功。

当你设置" link -> General -> Additional Library Directories"属性时,你只添加了一个链接器应该搜索V8库的路径。

将这些V8库添加到" link -> Input -> Additional Dependencies"属性中:

v8_base.lib;v8_libbase.lib;v8_libplatform.lib;v8_nosnapshot.lib