解决windows中msvc调用v8动态库的链接错误问题

windows平台下,v8默认是由clang编译的,而vs ide默认使用msvc编译,

由于导出函数中使用了std::unique_ptr,这个命名空间在clang中导出后会变,比如变成了std::__Cr::unique_ptr,

所以msvc调用clang生成的dll时就会链接不到函数出错,

下面用动态加载的方式处理

namespace v8::platform {
    typedef std::unique_ptr<v8::Platform>(*pfnNewDefaultPlatform)(
        int thread_pool_size, IdleTaskSupport idle_task_support,
        InProcessStackDumping in_process_stack_dumping,
        std::unique_ptr<v8::TracingController> tracing_controller,
        PriorityMode priority_mode);

    std::unique_ptr<v8::Platform> NewDefaultPlatform2(
        int thread_pool_size = 0,
        IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
        InProcessStackDumping in_process_stack_dumping = InProcessStackDumping::kDisabled,
        std::unique_ptr<v8::TracingController> tracing_controller = {},
        PriorityMode priority_mode = PriorityMode::kDontApply) {
        static v8::platform::pfnNewDefaultPlatform fnNewDefaultPlatform = nullptr;
        if (!fnNewDefaultPlatform) {
            auto mod = ::LoadLibraryA("v8_libplatform.dll");
            assert(mod);
            fnNewDefaultPlatform = (v8::platform::pfnNewDefaultPlatform)::GetProcAddress(mod, (LPCSTR)94);  //注意这个序号,去v8_libplatform.dll查看导出
            assert(fnNewDefaultPlatform);
        }
        return fnNewDefaultPlatform(thread_pool_size, idle_task_support, in_process_stack_dumping, {}, priority_mode);
    }

}  // namespace v8::platform
//v8::platform::NewDefaultPlatform()换成v8::platform::NewDefaultPlatform2()即可

// Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform2();
auto axxx = platform.get();
v8::V8::InitializePlatform(platform.get());
auto v8ret = v8::V8::Initialize();

error LNK2019: 无法解析的外部符号 “class std::unique_ptr<class v8::Platform,struct std::default_delete<class v8::Platform> > __cdecl v8::platform::NewDefaultPlatform(int,enum v8::platform::IdleTaskSupport,enum v8::platform::InProcessStackDumping,class std::unique_ptr<class v8::TracingController,struct std::default_delete<class v8::TracingController> >,enum v8::platform::PriorityMode)” (?NewDefaultPlatform@platform@v8@@YA?AV?$unique_ptr@VPlatform@v8@@U?$default_delete@VPlatform@v8@@@std@@@std@@HW4IdleTaskSupport@12@W4InProcessStackDumping@12@V?$unique_ptr@VTracingController@v8@@U?$default_delete@VTracingController@v8@@@std@@@4@W4PriorityMode@12@@Z),函数 _main 中引用了该符号

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注