Fan*_*eng 7 c++ linker-errors unresolved-external function-templates
我有一个包含两个文件的小项目:
主程序
#include <string>
template<int I>
int getint(int i)
{
extern std::string var;
return var.size() * i;
}
int main()
{
return getint<2>(2);
}
Run Code Online (Sandbox Code Playgroud)
和子.cpp
#include <string>
extern std::string var;
std::string var = "Hello";
Run Code Online (Sandbox Code Playgroud)
但是当我编译项目时,链接器给出了错误:
-------------- Clean: Debug in test (compiler: GNU GCC Compiler)---------------
Cleaned "test - Debug"
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\sub.cpp -o obj\Debug\sub.o
mingw32-g++.exe -o bin\Debug\test.exe obj\Debug\main.o obj\Debug\sub.o
obj\Debug\main.o: In function `Z6getintILi2EEii':
C:/Users/Fan/Downloads/test/test/main.cpp:6: undefined reference to `var'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Run Code Online (Sandbox Code Playgroud)
我不知道这是怎么发生的,因为只要我稍微简化一下程序,问题就会消失。
如果我让 getint() 成为一个普通的旧函数而不是一个函数模板,那么编译就会成功。
如果我移动“extern std::string var;”这一行 在函数之外(说到它上面的一行),然后编译成功。
如果我将 var 的类型更改为 int(并在 main.cpp 中省略“.size()”,并在 sub.cpp 中为其分配 1),则编译成功。
阻止我的原始项目编译的因素似乎是奇怪的组合。我不知道它们是什么:(
编辑:为了响应“无法复制”的评论,我正在尝试使用 Code::Blocks 17.12 在本地 Windows 7 机器上进行编译。编译器和链接器的输出已更新为包括编译器标志。