C++ - Qt + v8 under msvc2012

C++ - Qt + v8 under msvc2012

本文关键字:under msvc2012 v8 Qt C++      更新时间:2023-10-16

最近,我在Qt5.1.0下启动了一个项目。
经过一些开发,我选择在Javascript下使用Google V8制作一个脚本系统
Windows 7 x64下,编译V8的唯一方法是在msvc2012上,我有3个.lib文件要使用
在使用ONLY V8的单个项目中,一切都很好。但是,将V8与使用Qt5的现有项目集成起来就有点复杂了。

以下是我正在使用的最小代码的示例:(当然,这个项目中还有更多的文件…)

#include <QApplication>
#include <v8.h>
using namespace v8;
int v8_test() {
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope handle_scope(isolate);
  Handle<Context> context = Context::New(isolate);
  Persistent<Context> persistent_context(isolate, context);
  Context::Scope context_scope(context);
  Handle<String> source = String::New("'Hello' + ', World!'");
  Handle<Script> script = Script::Compile(source);
  Handle<Value> result = script->Run();
  persistent_context.Dispose();
  String::AsciiValue ascii(result);
  printf("%sn", *ascii);
  return 0;
}
int main(int ac, char **av)
{
    std::cout<<"Starting application"<<std::endl;
    QApplication app(ac, av);
    v8_test();
    //Do something else
    return app.exec();
}

在这一点上,我得到了很多这种类型的链接错误:

1>v8_base.x64.lib(api.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in moc_aCertainFile.obj
1>v8_base.x64.lib(v8threads.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in moc_aCertainFile.obj
1>v8_base.x64.lib(checks.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in moc_aCertainFile.obj

似乎Qt是用/MDd标志编译的,V8只能用/MTd标志编译。

经过大量的研究和测试,我什么都找不到
有人知道解决这个问题的线索吗?

提前谢谢。

Google V8默认使用MT标志构建,因此与使用MD标志构建的Qt不兼容。

让它用Qt构建V8的诀窍是在visual studio中用MD标志进行V8编译。你可以用以下方法做到这一点:

  1. 通过执行build\gyp_v8.py生成Visual Studio项目
  2. 使用Visual Studio打开all.sln
  3. 在解决方案的所有项目中,设置MD标志:属性>C/C++>代码生成>运行库>多线程DLL(/MD)
  4. 之后重建解决方案
  5. 您将获得与QtCreator一起使用的正确lib文件

在QtCreator中,继续进行V8库的链接:

LIBS += -L/PATH_TO_LIBRARIES/ -lv8_base.ia32 -licui18n -licuuc -lv8_nosnapshot.ia32 -lv8_snapshot

您还需要链接到WinMM.libWS2_32.libadvapi32.lib,它们通常位于:C:\Program Files(x86)\Windows Kits\

这让它出现在我的系统中。我希望它能帮助其他有同样问题的人。

嗯,我无法以这种方式使用V8和Qt5,即使在多次尝试以静态方式构建Qt之后也是如此。

因此,我为V8编写了一个.dll包装器,它可以集成到我在QtCreator上的项目中。

这是我的包装纸:

包装测试.hh:

#ifndef WRAPTEST_HH_
#define WRAPTEST_HH_
#include <iostream>
namespace v8w {
    class WrapTest {
    public:
        static __declspec(dllexport) void   hello();
    };
}
#endif /* WRAPTEST_HH_ */

包装测试.cpp:

#include <v8.h>
#include "WrapTest.hh"
void    v8w::WrapTest::hello() {
    std::cout<<"Hello, i'm V8 wrapper! :D"<<std::endl;
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::HandleScope handle_scope(isolate);
    v8::Handle<v8::Context> context = v8::Context::New(isolate);
    v8::Persistent<v8::Context> persistent_context(isolate, context);
    v8::Context::Scope context_scope(context);
    v8::Handle<v8::String> source = v8::String::New("'Hello' + ', World!'");
    v8::Handle<v8::Script> script = v8::Script::Compile(source);
    v8::Handle<v8::Value> result = script->Run();
    persistent_context.Dispose();
    v8::String::AsciiValue ascii(result);
    printf("%sn", *ascii);
    std::cout<<"End v8w::WrapTest::hello()"<<std::endl;
}

我有WrapTest.hhV8_Wrapper.libV8 _Wrapper.dll,并将.lib添加到我的.pro文件中到我的Qt5项目中:

LIBS += -L"$$_PRO_FILE_PWD_/lib"
        -lV8_Wrapper

在我的Qt项目中,主.cpp文件:

#include <iostream>
#include <QApplication>
#include "WrapTest.hh"
void testV8() {
    std::cout<<"test"<<std::endl;
    v8w::WrapTest::hello();
}
int main(int ac, char **av) {
    std::cout<<"Starting application"<<std::endl;
    QApplication app(ac, av);   
    testV8();
    return app.exec();
}

这给了我标准输出:

Starting application
test
Hello, i'm V8 wrapper! :D
Hello, World!
End v8w::WrapTest::hello()

我希望这个解决方案可以帮助你,如果你需要^_^