简体   繁体   English

未定义的对显式实例化模板函数的引用

[英]Undefined reference to explicitly instantiated template function

I have a class with a template method that is declared in a header and defined in a cpp file, so 我有一个带有在标头中声明并在cpp文件中定义的模板方法的类,因此

// A.h - declaration
struct A
{
    template<typename T = int> void fun();
};

and then 接着

// A.cpp - definition
#include <iostream>
#include "A.h"

template<typename T> void A::fun()
{
    std::cout << "Hello world template" << std::endl;
}

// Explicit instantiation
template void A::fun<int>();

Now, somewhere else, in a test, I have 现在,在其他地方,在测试中

#include "A.h"

int main ()
{
    A a;
    a.fun();
}

and since I explicitly instantiated the function for int template parameter, all is well. 由于我显式实例化了int模板参数的函数,因此一切都很好。 Or should be! 还是应该的!

My code links fine with GCC 7.3.0 but fails to link with clang 6, which reports an undefined reference to function fun . 我的代码可以与GCC 7.3.0很好地链接,但是不能与clang 6链接,clang 6报告了对fun函数的未定义引用。

What's going on here? 这里发生了什么? Is there anything else that I should take care of? 还有什么我需要照顾的吗? Perhaps something that GCC takes for granted while clang doesn't? 也许GCC会把clang视为理所当然的事情吗?

Thanks! 谢谢!

Edit 编辑

@WhozCraig, @Neil Butterworth here are the linking commands for the two toolchains. @ WhozCraig,@ Neil Butterworth是这两个工具链的链接命令。

GCC: GCC:

/usr/bin/g++  -g   CMakeFiles/test_generator.dir/generator/generator.cpp.o CMakeFiles/test_generator.dir/test_utils.cpp.o  -o test_generator -Wl,-rpath,/home/mleoni/PhD/ABI/libcellml/build/src:/home/mleoni/PhD/ABI/libcellml/build/tests/gtest ../src/libcellmld.so.0.1.0 gtest/libgtest_main.so /usr/lib/x86_64-linux-gnu/libxml2.so gtest/libgtest.so -lpthread

clang: 铛:

/usr/bin/clang++  -g   CMakeFiles/test_generator.dir/generator/generator.cpp.o CMakeFiles/test_generator.dir/test_utils.cpp.o  -o test_generator -Wl,-rpath,/home/mleoni/PhD/ABI/libcellml/build/src:/home/mleoni/PhD/ABI/libcellml/build/tests/gtest ../src/libcellmld.so.0.1.0 gtest/libgtest_main.so /usr/lib/x86_64-linux-gnu/libxml2.so gtest/libgtest.so -lpthread

@IdeaHat: that didn't resolve the issue unfortunately, I am still getting the same error @IdeaHat:不幸的是,这并没有解决问题,我仍然遇到相同的错误

Edit 2 Since the code I am actually trying to compile is [obviously] not that one, I tried to compile this MWE and both GCC and clang link it fine with just cc A.cpp main.cpp . 编辑2由于我实际上试图编译的代码不是(显然)不是那种代码,所以我尝试编译此MWE,并且GCC和clang仅使用cc A.cpp main.cpp就可以很好地链接它。

I am now trying to understand what's the difference between this minimal case and the code that I actually have. 我现在正在尝试了解这种最小用例和我实际拥有的代码之间的区别。

I found it. 我找到了。 Somewhere in this code base I am not familiar with all classes are marked private by default [as in Windows D:] and after I marked my class public clang could finally link correctly. 在此代码库中的某个地方,我不熟悉所有类,默认情况下它们都标记为私有(如在Windows D:中一样),并且在标记了类之后,公共clang最终可以正确链接。

Now the plot thickens because with the binaries compiled by clang I see a lot of segfaults in the test suite that are not present when compiled with GCC. 现在该图变厚了,因为使用clang编译的二进制文件使我看到测试套件中有很多段错误,而使用GCC编译时这些段不存在。

Also, now I have the same question as the one in this thread, except now GCC and clang work fine while MSVC says unresolved external symbol :'( . 另外,现在我和该线程中的问题相同,只是现在GCC和clang可以正常工作,而MSVC表示unresolved external symbol :'(。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM