C++函数模板float特化时出现多重定义链接错误
函数模板多重定义链接故障
你正在开发一套函数模板,目前编译可正常通过,可在Compiler Explorer上查看可运行的示例。问题在于,当你在项目中尝试将模板的类型实参T设为float使用时,会因多重定义导致链接失败,尝试了多种方案都没能解决。
现有代码结构
cstr.h 头文件
#pragma once #include <stdint.h> const uint8_t default_decimal_places = 8; template<typename T> const char* to_string(T value) { static char output_buffer[128]; uint8_t negative = 0; bool isNegative = false; if (value < 0) { isNegative = true; negative = 1; value *= -1; output_buffer[0] = '-'; } uint8_t size = 0; T sizeTest = value; while (sizeTest / 10 > 0) { sizeTest /= 10; size++; } uint8_t idx = 0; if (isNegative) { while (value / 10 > 0) { uint8_t remainder = value % 10; value /= 10; output_buffer[negative + size - idx] = remainder + '0'; idx++; } uint8_t remainder = value % 10; output_buffer[negative + size - idx] = remainder + '0'; output_buffer[negative + size + 1] = 0; } else { while (value / 10 > 0) { uint8_t remainder = value % 10; value /= 10; output_buffer[size - idx] = remainder + '0'; idx++; } uint8_t remainder = value % 10; output_buffer[size - idx] = remainder + '0'; output_buffer[size + 1] = 0; } return output_buffer; } template<typename T> const char* to_string(T value, uint8_t decimal_places) { if (decimal_places > 20) decimal_places = 20; static char output_double_buffer[128]; char* intPtr = (char*)to_string((int64_t)value); char* doublePtr = output_double_buffer; if (value < 0) { value *= -1; } while(*intPtr != 0) { *doublePtr = *intPtr; intPtr++; doublePtr++; } *doublePtr = '.'; doublePtr++; T newValue = value - (int)value; for (uint8_t i = 0; i < decimal_places; i++) { newValue *= 10; *doublePtr = (int)newValue + '0'; newValue -= (int)newValue; doublePtr++; } *doublePtr = 0; return output_double_buffer; } template<> inline const char* to_string<float>(float value, uint8_t decimal_places) { if (decimal_places > 14) decimal_places = 14; static char output_float_buffer[128]; char* intPtr = (char*)to_string((int64_t)value); char* floatPtr = output_float_buffer; if (value < 0) { value *= -1; } while(*intPtr != 0) { *floatPtr = *intPtr; intPtr++; floatPtr++; } *floatPtr = '.'; floatPtr++; float newValue = value - (int)value; for (uint8_t i = 0; i < decimal_places; i++) { newValue *= 10; *floatPtr = (int)newValue + '0'; newValue -= (int)newValue; floatPtr++; } *floatPtr = 'f'; floatPtr++; *floatPtr = 0; return output_float_buffer; }
对应的cstr.cpp实现仅包含头文件引用:
#include "cstr.h"
BasicRenderer.cpp 模板调用实现
void BasicRenderer::Print(double val, uint8_t decimal_places) { Print(to_string(val, decimal_places)); } void BasicRenderer::Print(float val, uint8_t decimal_places) { Print(to_string(val, decimal_places)); }
链接阶段报错信息
执行构建流程时,最终在链接环节触发如下错误:
skilz420@skilz-PC:~/skilzOS/kernel$ make kernel !==== COMPILING src/cstr.cpp gcc -ffreestanding -fshort-wchar -c src/cstr.cpp -o lib/cstr.o !==== LINKING ld -T kernel.ld -static -Bsymbolic -nostdlib -o bin/kernel.elf lib/kernel.o lib/cstr.o lib/BasicRenderer.o ld: lib/BasicRenderer.o: in function `char const* to_string<float>(float, unsigned char)': BasicRenderer.cpp:(.text+0x0): multiple definition of `char const* to_string<float>(float, unsigned char)'; lib/kernel.o:kernel.cpp:(.text+0x0): first defined here make: *** [Makefile:33: link] Error 1 skilz420@skilz-PC:~/skilzOS/kernel$
问题特征与已尝试方案
- 已尝试的修复操作包括:将
float特化的实现移到CPP文件、给特化函数添加inline修饰,均未解决故障 - 仅使用
double类型的模板版本可正常编译链接,只要添加float版本的特化实现(float的处理逻辑确实与double存在差异)就会立刻触发多重定义错误
内容的提问来源于stack exchange,提问作者Francis Cugler
相关产品推荐
相关产品推荐

