I have a simple code that’s trying to link to a static method of a class template that’s already instantiated in a shared library. This is the code:
<code>#include <sym/rot3.h>
sym::Rot3<float>::FromYawPitchRoll(0, 0, 1);
<code>#include <sym/rot3.h>
int main() {
sym::Rot3<float>::FromYawPitchRoll(0, 0, 1);
}
</code>
#include <sym/rot3.h>
int main() {
sym::Rot3<float>::FromYawPitchRoll(0, 0, 1);
}
I compile it with:
<code>g++ -I /home/lukasz/anaconda3/envs/anaconda/include -I /home/lukasz/eigen -L/home/lukasz/anaconda3/envs/anaconda/lib -l symforce_gen -v -std=c++14 main.cpp
<code>g++ -I /home/lukasz/anaconda3/envs/anaconda/include -I /home/lukasz/eigen -L/home/lukasz/anaconda3/envs/anaconda/lib -l symforce_gen -v -std=c++14 main.cpp
</code>
g++ -I /home/lukasz/anaconda3/envs/anaconda/include -I /home/lukasz/eigen -L/home/lukasz/anaconda3/envs/anaconda/lib -l symforce_gen -v -std=c++14 main.cpp
The compilation step works fine, but there’s an error during linking:
<code>/usr/bin/ld: /tmp/ccJ5lt02.o: in function `main':
main.cpp:(.text+0x11a9): undefined reference to `sym::Rot3<float>::FromYawPitchRoll(float, float, float)'
<code>/usr/bin/ld: /tmp/ccJ5lt02.o: in function `main':
main.cpp:(.text+0x11a9): undefined reference to `sym::Rot3<float>::FromYawPitchRoll(float, float, float)'
</code>
/usr/bin/ld: /tmp/ccJ5lt02.o: in function `main':
main.cpp:(.text+0x11a9): undefined reference to `sym::Rot3<float>::FromYawPitchRoll(float, float, float)'
The code for static method in question (Rot3::FromYawPitchRoll) is available in the shared library symforce_gen that I’ve linked in my invocation of gcc. It can be verified by compiling the main.cpp to main.o and looking up the unresolved symbol for this method inside main.o:
<code>nm main.o | grep -i yaw
U _ZN3sym4Rot3IfE16FromYawPitchRollEfff
<code>nm main.o | grep -i yaw
U _ZN3sym4Rot3IfE16FromYawPitchRollEfff
</code>
nm main.o | grep -i yaw
U _ZN3sym4Rot3IfE16FromYawPitchRollEfff
This is matching exactly what is available in the shared library I provided:
<code>nm -D /home/lukasz/anaconda3/envs/anaconda/lib/libsymforce_gen.so | grep -i _ZN3sym4Rot3IfE16FromYawPitchRollEfff
0000000000069430 W _ZN3sym4Rot3IfE16FromYawPitchRollEfff
<code>nm -D /home/lukasz/anaconda3/envs/anaconda/lib/libsymforce_gen.so | grep -i _ZN3sym4Rot3IfE16FromYawPitchRollEfff
0000000000069430 W _ZN3sym4Rot3IfE16FromYawPitchRollEfff
</code>
nm -D /home/lukasz/anaconda3/envs/anaconda/lib/libsymforce_gen.so | grep -i _ZN3sym4Rot3IfE16FromYawPitchRollEfff
0000000000069430 W _ZN3sym4Rot3IfE16FromYawPitchRollEfff
Why can’t the linker see this method?