OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
OSX clang++: Undefined symbols for architecture x86_64 for explicitely instantiated template in cpp file
我在.h文件中定义了模板类,并在.cpp文件中定义了模板方法。此.cpp文件还包含通过template clas Class<type>
显式模板实例化。
此用例在VS2019上与GCC(7.4.0(相同,可以正常工作。但是,它不适用于带有clang++的OSX(Apple LLVM版本10.0.0 clang-1000.11.45.5(。
根据文档,我相信这是一个有效的代码。有没有办法让它在 OSX 叮当声下工作?
我不想将所有实现都移动到 .h,因为更好的可读性,因为我只需要两个/三个模板实例化。
这是我的测试文件:
test.h
#pragma once
template <class T>
class CTemplateTest
{
public:
int Test();
};
test.cpp
#include "test.h"
template class CTemplateTest<int>;
template class CTemplateTest<double>;
template <class T>
int CTemplateTest<T>::Test()
{
return 42;
}
main.cpp
#include "test.h"
int main(int argc, char** argv)
{
CTemplateTest<int> t1;
CTemplateTest<double> t2;
t1.Test();
t2.Test();
}
output
Undefined symbols for architecture x86_64:
"CTemplateTest<double>::Test()", referenced from:
_main in main.o
"CTemplateTest<int>::Test()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
感谢您的任何帮助。
成员函数未实例化。这并不奇怪,因为您在定义CTemplateTest<T>::Test
之前执行显式实例化。将显式实例化移到test.cpp
末尾
template <class T>
int CTemplateTest<T>::Test()
{
return 42;
}
template class CTemplateTest<int>;
template class CTemplateTest<double>;
我建议你在标头中添加一个显式的实例化声明
template <class T>
class CTemplateTest
{
public:
int Test();
};
extern template class CTemplateTest<int>;
extern template class CTemplateTest<double>;
这指示编译器在使用特定专用化时放弃大部分隐式实例化。它会知道完整的定义在别处。
此外,它还具有良好的文档目的。现在只需读取标头即可知道支持的类型。
好的,我会回答我的问题。
由于我不明白的原因,clang++ 在所有其他代码之后需要这些显式实例化。
因此,test.cpp
文件的正确形式是:
#include "test.h"
template <class T>
int CTemplateTest<T>::Test()
{
return 42;
}
template class CTemplateTest<int>;
template class CTemplateTest<double>;
我希望这也对其他人有所帮助!
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 体系结构x86_64的未定义符号:链接器错误
- C++ std::vector<int> 体系结构的未定义符号 x86_64:
- 未使用的 asm() 在不受支持的体系结构上的行为
- 用于本地网络运行的客户端服务器体系结构
- 生成文件:体系结构x86_64的未定义符号
- Xcode"体系结构x86_64的未定义符号"
- 有了memory_order_relaxed,原子变量的总修改顺序如何在典型体系结构上得到保证
- 体系结构x86_64的未定义符号:找不到原因
- Apple Mach-O-Linker 错误:在体系结构 x86-64 中找不到从(行)引用的变量
- 在英特尔x86体系结构上使用非AVX指令修改xmm整数寄存器值
- X86体系结构中的CPUID
- arm和x86体系结构中的c/c++语言有什么不同
- 使用c++以及x86和x64体系结构访问处理器中断
- x86 体系结构上的内存排序限制
- 为什么是32位x86体系结构上“int64_t”8字节的默认对齐方式