C++ - Qt + v8 under msvc2012
C++ - Qt + v8 under msvc2012
最近,我在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编译。你可以用以下方法做到这一点:
- 通过执行build\gyp_v8.py生成Visual Studio项目
- 使用Visual Studio打开all.sln
- 在解决方案的所有项目中,设置MD标志:属性>C/C++>代码生成>运行库>多线程DLL(/MD)
- 之后重建解决方案
- 您将获得与QtCreator一起使用的正确lib文件
在QtCreator中,继续进行V8库的链接:
LIBS += -L/PATH_TO_LIBRARIES/ -lv8_base.ia32 -licui18n -licuuc -lv8_nosnapshot.ia32 -lv8_snapshot
您还需要链接到WinMM.lib
、WS2_32.lib
和advapi32.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.hh、V8_Wrapper.lib和V8 _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()
我希望这个解决方案可以帮助你,如果你需要^_^
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- MSVC2012 with Visual Studio 2017
- Cppcheck 静态代码分析器实际上可以检测到不太常见的警告(如 "Relative Path Traversal (CWE-23)" 或"Buffer Under-read(CWE-127)")吗
- 如何使用 C++ 和 Matlab Engine under Kdevelopwith CMakeList.
- 数学背后的"compute n! under modulo p"?
- C++内存分配使用Under Green Hills INTEGRITY
- CommandBar under WIN CE6
- "under the hood"性能理解的必读C++文档?
- std::函数复制运算符 (MSVC2012) 的奇怪行为
- 可视化GDB在MSVC2012和GCC 7.3.1之间打印不同的C++输出
- TCHAR and _tprintf under Mac OSX
- 检查MSVC2012中声明的成员函数
- emplace_back() issue under VS2013
- GetVersionEx under Windows 8
- Qt5:此代码使用msvc2012编译正常,但使用MinGW编译时出现错误..为什么?
- C++ - Qt + v8 under msvc2012
- OpenGL under QML
- std::greater在MSVC2012中未定义
- 如何开始数学挑战"find the sum of the multiples of X under Y"?
- 当文件有换行符时,ifstream::tellg在MSVC2012和gcc(mingw)上有所不同