C ++-msvc2012下的Qt + v8 [英] C++ - Qt + v8 under msvc2012

查看:253
本文介绍了C ++-msvc2012下的Qt + v8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在 Qt5.1.0 下启动了一个项目.
经过一些开发后,我选择使用 Google V8 Javascript 下创建脚本系统.
Windows 7 x64 下,编译 V8 的唯一方法是在 msvc2012 下,并且我有3个.lib文件可供使用.
仅使用V8 的单个项目中,一切正常.但是将V8与使用Qt5的现有项目集成起来要复杂一些.

Recently, I've started a project under Qt5.1.0.
After some development, I choose to make a scripting system under Javascript with Google V8.
Under Windows 7 x64, the only way to compile V8 is under msvc2012, and I got 3 .lib files to use.
In a single project using ONLY V8, everything works well. But integrating V8 with an existent project using Qt5 it's a bit more complicated.

这是我正在使用的最少代码的一个示例:(当然,该项目中还有更多文件...)

Here is an example of a minimal code I'm using : (Of course, there is more file in this project...)

#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("%s\n", *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();
}

这时,我遇到了很多这种类型的链接错误:

At this point, I got a lot of linking errors of this type :

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标志编译的.

It seems that Qt was compiled with /MDd flag, and V8 can only be compiled /MTd flag.

经过大量的研究和测试,我找不到任何东西...
有人有解决此问题的线索吗?

After lot of research and testing, I was unable to find anything...
Anyone got a clue to resolve this problem?

谢谢.

推荐答案

好吧,即使经过很多尝试以静态方式构建Qt,我也无法以这种方式使用V8和Qt5.

Well, I was unable to use V8 and Qt5 in this way, even after many tries to build Qt in static.

因此,我为V8编写了.dll包装器,可以将其集成到QtCreator上的项目中.

So, I wrote a .dll wrapper for V8, which can be integrated into my project on QtCreator.

这是我的包装纸:

WrapTest.hh:

WrapTest.hh:

#ifndef WRAPTEST_HH_
#define WRAPTEST_HH_

#include <iostream>

namespace v8w {

    class WrapTest {
    public:
        static __declspec(dllexport) void   hello();
    };
}

#endif /* WRAPTEST_HH_ */

WrapTest.cpp:

WrapTest.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("%s\n", *ascii);
    std::cout<<"End v8w::WrapTest::hello()"<<std::endl;
}

我有 WrapTest.hh V8_Wrapper.lib V8_Wrapper.dll ,并将.lib添加到我的.pro中归档到我的Qt5项目中:

I've got WrapTest.hh, V8_Wrapper.lib and V8_Wrapper.dll, and a add the .lib to my .pro file into my Qt5 project :

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

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

In my Qt project, the main.cpp file :

#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();
}

哪个在标准输出中给了我:

Which gave me in the standard output :

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

如果您需要^ _ ^

I hope this solution can help you if you're in the need ^_^

这篇关于C ++-msvc2012下的Qt + v8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆