使用MSVC 2013和2015,使用CMake和ExternalProject_Add,通过引用导出的成员函数传递st
Passing std::vector by reference to Exported Member Function using MSVC 2013 and 2015, Using CMake and ExternalProject_Add
我有一个在Linux和Windows上构建的动态库。在两个系统下针对静态库运行单元测试时,我没有遇到任何问题。当在linux中对我的共享库运行单元测试时,一切都很好。在Windows中针对共享库运行单元测试时,出现了问题。
总之,我的堆栈跟踪如下:UnitTest.exe!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1424 C++
UnitTest.exe!_free_dbg(void * pUserData, int nBlockUse) Line 1265 C++
UnitTest.exe!operator delete(void * pUserData) Line 54 C++
UnitTest.exe!std::allocator<QuadKey::QuadKey>::deallocate(QuadKey::QuadKey * _Ptr, unsigned __int64 __formal) Line 574 C++
UnitTest.exe!std::_Wrap_alloc<std::allocator<QuadKey::QuadKey> >::deallocate(QuadKey::QuadKey * _Ptr, unsigned __int64 _Count) Line 859 C++
UnitTest.exe!std::vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >::_Tidy() Line 1629 C++
UnitTest.exe!std::vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >::~vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >() Line 946 C++
UnitTest.exe!QuadKey::BINGSYSTEM_GetChildren_Test::TestBody() Line 195 C++
在QuadKey::BINGSYSTEM_GetChildren_Test::TestBody()
体中调用的导出成员函数接受一个数组的引用,将其传递给另一个类中的非导出函数并返回。如:
void QuadKey::getChildren(std::vector<QuadKey> &outKeys) const
{
m_Impl->getChildren(outKeys, (*this));
}
,
// m_Impl getChildren System class is not exported.
void System::getChildren(std::vector<QuadKey> &outKeys,
const QuadKey &self) const
{
for (std::uint8_t quadrant = 0; quadrant < 4; ++quadrant) {
QuadKey child =
getChild(static_cast<QuadKey::Quadrant>(quadrant), self);
Detail::insertVectorIfValidAndUnique(outKeys, child);
}
}
在使用MSVC时,是否可以安全地假设这种做法不是标准的?那是在传递一个std::vector
到一个导出的函数?如果不是,我该假设什么呢?当向量超出单元测试体的作用域而被释放时,就会发生断言。我试图在矢量上调用clear,但即使在空std::vector
上也会发生相同的崩溃。
修改:
改变我的UnitTest和动态库使用MD (MDd调试)而不是MT (MTd调试)我得到一个错误:
Error LNK2038 mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj UnitTest C:UsersmehogganDevelQuadKeysbuildUnitTestlibcpmtd.lib(StlCompareStringA.obj) 1
这是因为gtest在默认情况下是使用针对标准运行时的静态链接编译的。如果我不想改变gtest,那么在编写需要跨越dll边界的标准容器的接口时,什么将是良好的c++实践?
这是因为你静态地链接到C运行时。您需要与DLL版本链接。构建/MD
,而不是/MT
。
请参阅跨DLL边界传递CRT对象的潜在错误,以了解可能导致的问题。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用新行和不使用新行读取文件
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 使用CMake创建QML插件
- 使用strcpy将char数组的元素复制到另一个数组
- 如何使用set<pair<int,int> >::iterator itrator it迭代set<pair<int,int> >st中的值?
- 使用 FFMPEG 将每个输入图像转换为 H264 编译时出错,该图像在 MevisLab 中运行的 Visual St
- 使用MSVC 2013和2015,使用CMake和ExternalProject_Add,通过引用导出的成员函数传递st