<div dir="ltr"><div dir="ltr">On Mon, 30 Mar 2020 at 14:19, Alexis Murzeau via cfe-users <<a href="mailto:cfe-users@lists.llvm.org">cfe-users@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
When using clang, I discovered that it errors out where other compilers <br>
doesn't (GCC and MSVC).<br>
<br>
I'm trying to do this:<br>
 - Have a library compiled with -fvisibility=hidden and adding __attribute__((visibility("default")))<br>
   only for stuff that must be exported from the library .so.<br>
<br>
 - Have an exported class (with __attribute__((visibility("default")))) that<br>
   declares a member function template<br>
<br>
 - A cpp file in the library does does instantiation of that member function<br>
   template for all applicable types that would ever be usable by that member function template.<br></blockquote><div><br></div><div>If you want one source file to provide instantiations for use by a different source file, it's not sufficient to merely trigger those instantiations. That only generates a discardable definition of the instantiation, and if (for example) that definition is inlined, no symbol will be provided for other translation units to link against. Instead you need to use an explicit instantiation. (See <a href="https://en.cppreference.com/w/cpp/language/function_template#Explicit_instantiation">https://en.cppreference.com/w/cpp/language/function_template#Explicit_instantiation</a>)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
When compiling that library, the member function template instantiations are<br>
not being exported from the library (they are hidden).<br>
<br>
The error comes then when an executable try to use that function, but<br>
compilation fails because of undefined reference to `void NetworkPacketLogger::logType<T1>(T1 const*)'.<br>
<br>
<br>
In a lib.h:<br>
```<br>
// An enum defining the network packet<br>
enum class Opcode {<br>
        T1,<br>
        T2<br>
};<br>
<br>
// Base class<br>
struct NetworkPacket {<br>
        NetworkPacket(enum Opcode t) : type(t) {}<br>
<br>
        enum Opcode type;<br>
};<br>
<br>
// Sample of possible network packets<br>
struct T1 : public NetworkPacket {<br>
        T1() : NetworkPacket(Opcode::T1) {}<br>
        int value;<br>
};<br>
<br>
struct T2 : public NetworkPacket {<br>
        T2() : NetworkPacket(Opcode::T2) {}<br>
        float value;<br>
};<br>
<br>
// Class to log a network packet<br>
class __attribute__((visibility("default"))) NetworkPacketLogger {<br>
public:<br>
        // "Slow" function that find the correct packet opcode and log it's content<br>
        // vvv this function is exported just because of the attribute on the NetworkPacketLogger, OK<br>
        static void logAbstractType(struct NetworkPacket* abstractData); <br>
<br>
        // Fast function that doesn't have to find the packet opcode<br>
        // It does just a log of data->value<br>
        // T can only be either T1 or T2<br>
<br>
        // vvv this is the hidden function that should be exported from the lib<br>
        template<class T> static void logType(const T* data) __attribute__((visibility("default")));<br>
};<br>
```<br>
<br>
In a lib.cpp:<br>
```<br>
template<class T><br>
void NetworkPacketLogger::logType(const T* data) {<br>
        std::cout << data->value;<br>
}<br>
<br>
void NetworkPacketLogger::logAbstractType(struct NetworkPacket* abstractData) {<br>
        // Possibly generated code is there are many possible network packets<br>
        // This will implicitely instanciate all possible combination of NetworkPacketLogger::logType<T><br>
        // I want these instanciations to be available by user of the library<br></blockquote><div><br></div><div>To make these available to users, you should explicitly instantiate them as follows:</div><div><br></div><div>template void NetworkPacketLogger::logType(const T1*);</div><div>template void NetworkPacketLogger::logType(const T2*);</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
        switch(abstractData->type) {<br>
                case Opcode::T1:<br>
                        logType(static_cast<T1*>(abstractData));<br>
                        break;<br>
                case Opcode::T2:<br>
                        logType(static_cast<T2*>(abstractData));<br>
                        break;<br>
        }<br>
}<br>
```<br>
<br>
<br>
I found by tweaking the code that, if I add a __attribute__((visibility("default")))<br>
on struct T1 and struct T2, then logAbstractType<T1> and logAbstractType<T2> are exported.<br>
<br>
But why is this required for clang ?<br>
It seems to be like this old bug: <a href="https://bugs.llvm.org/show_bug.cgi?id=8457" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=8457</a><br>
<br>
Is this expected ?<br></blockquote><div><br></div><div>Yes. You're probably just getting lucky with the other compilers, and they happen to not inline either of the 'logType' functions.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Shouldn't a warning be emitted when a function that should be "visibility("default")"<br>
is not because of one of the arguments use a struct with visibility("hidden") ?<br>
<br>
<br>
I'm attaching a test case.<br>
Can be compiled with something like this:<br>
```<br>
mkdir build && cd build<br>
CC=clang-9 CXX=clang++-9 cmake ..<br>
make<br>
nm -CD liblib.so  | grep logType<br>
```<br>
<br>
nm will print only this (no NetworkPacketLogger::logType<T1>):<br>
00000000000011b0 T NetworkPacketLogger::logAbstractType(NetworkPacket*)<br>
0000000000001260 W void NetworkPacketLogger::logType<T2>(T2 const*)<br>
0000000000001290 W void NetworkPacketLogger::logType<T3>(T3 const*)<br>
<br>
<br>
Thanks for your hindsight :)<br>
<br>
-- <br>
Alexis Murzeau<br>
PGP: B7E6 0EBB 9293 7B06 BDBC  2787 E7BD 1904 F480 937F<br>
_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><br>
</blockquote></div></div>