C++ 为什么静态库中的模板函数链接到动态库时会崩溃?

C++ 为什么静态库中的模板函数链接到动态库时会崩溃?,c++,xcode,C++,Xcode,在使用XCode 11.6的OSX上,我将v8构建为一个静态库(libv8_monolith.a)。在一种情况下,我将其链接到一个可执行文件中,一切正常;在另一种情况下,我将其链接到一个捆绑包(动态库)中,模板化函数崩溃EXC_BAD_访问: 例如分配页面(): 内存分配器 template <MemoryAllocator::AllocationMode alloc_mode = kRegular, typename SpaceType> EXPORT_TEMPLATE_DECL

在使用XCode 11.6的OSX上,我将v8构建为一个静态库(libv8_monolith.a)。在一种情况下,我将其链接到一个可执行文件中,一切正常;在另一种情况下,我将其链接到一个捆绑包(动态库)中,模板化函数崩溃EXC_BAD_访问:

例如分配页面()

内存分配器

template <MemoryAllocator::AllocationMode alloc_mode = kRegular, typename SpaceType> EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* AllocatePage(size_t size, SpaceType* owner, Executability executable);

extern template EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);
请注意,这些崩溃的模板化函数都不是对外公开的(它们不是v8.h API的一部分),它们都是v8内部的代码,位于其“名称空间内部”


是否缺少一些编译器或链接器标志?这是我应该做的吗?我必须将v8编译为动态库才能在另一个动态库中使用吗?

事实证明,After Effects内部有自己的v8动态库,因此当After Effects加载我们的动态库时,某些函数之间存在名称冲突。我现在的解决方案是使用更改的名称空间重新编译v8,以避免冲突。

事实证明,After Effects内部有自己的v8动态库,因此当After Effects加载我们的动态库时,某些函数之间存在名称冲突。我现在的解决方案是用更改的名称空间重新编译v8,以避免冲突。

您的
.cpp
文件中有函数模板的实现吗?你有调试器吗?你能描述一下车祸发生的方式和地点吗?(你应该有一个调用堆栈等等)你所说的“外部公开”到底是什么意思?哪里定义了
ConcurrentBitmap
?什么代码调用
标记位图
?返回值做了什么?@Yakk AdamNevraumont我用一个更好的例子和崩溃的屏幕截图更新了这个问题。您的
.cpp
文件中有函数模板的实现吗?你有调试器吗?你能描述一下车祸发生的方式和地点吗?(你应该有一个调用堆栈等等)你所说的“外部公开”到底是什么意思?哪里定义了
ConcurrentBitmap
?什么代码调用
标记位图
?返回值做了什么?@Yakk AdamNevraumont我用一个更好的例子和崩溃的屏幕截图更新了这个问题。
template <MemoryAllocator::AllocationMode alloc_mode, typename SpaceType>
Page* MemoryAllocator::AllocatePage(size_t size, SpaceType* owner, Executability executable) {
  MemoryChunk* chunk = nullptr;
  if (alloc_mode == kPooled) {
    DCHECK_EQ(size, static_cast<size_t>(
                        MemoryChunkLayout::AllocatableMemoryInMemoryChunk(
                            owner->identity())));
    DCHECK_EQ(executable, NOT_EXECUTABLE);
    chunk = AllocatePagePooled(owner);
  }
  if (chunk == nullptr) {
    chunk = AllocateChunk(size, size, executable, owner);
  }
  if (chunk == nullptr) return nullptr;
  return owner->InitializePage(chunk);
}

template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);
Page* AllocatePage2(size_t size, PagedSpace* owner, Executability executable);

Page* MemoryAllocator::AllocatePage2(size_t size, PagedSpace* owner, Executability executable) {
  MemoryChunk* chunk = nullptr;
  if (chunk == nullptr) {
    chunk = AllocateChunk(size, size, executable, owner);
  }
  if (chunk == nullptr) return nullptr;
  return owner->InitializePage(chunk);
}