~williamvds/linking-by-example

8757c7370d2a9f29b5b6414275325819d9fc69bc — williamvds 2 months ago d6cb4e3 main
Resolve undefined parent class method
7 files changed, 53 insertions(+), 12 deletions(-)

M CMakeLists.txt
R library.cpp => library.tpp
M other_library.cpp
M results/build log
A results/executable symbols
A results/exit code
M results/other_library.cpp.o symbols
M CMakeLists.txt => CMakeLists.txt +0 -1
@@ 5,7 5,6 @@ project(example
)

add_executable(example
    library.cpp
    other_library.cpp
    main.cpp
)

R library.cpp => library.tpp +0 -0
M other_library.cpp => other_library.cpp +2 -1
@@ 1,7 1,8 @@
#include "other_library.hpp"

#include "library.tpp"

template <typename T>
int ChildClass<T>::method() { return 1; }

template class ChildClass<int>;
template class TemplatedClass<int>;

M results/build log => results/build log +3 -9
@@ 3,12 3,6 @@ $ cmake -B build
-- Generating done (0.0s)
-- Build files have been written to: /home/william/scratch/public/linking-by-example/build
$ cmake --build build
[1/4] Building CXX object CMakeFiles/example.dir/library.cpp.o
[2/4] Building CXX object CMakeFiles/example.dir/other_library.cpp.o
[3/4] Building CXX object CMakeFiles/example.dir/main.cpp.o
[4/4] Linking CXX executable example
FAILED: example 
: && /usr/bin/c++   CMakeFiles/example.dir/library.cpp.o CMakeFiles/example.dir/other_library.cpp.o CMakeFiles/example.dir/main.cpp.o -o example   && :
/usr/bin/ld: CMakeFiles/example.dir/other_library.cpp.o:(.data.rel.ro._ZTV14TemplatedClassIiE[_ZTV14TemplatedClassIiE]+0x20): undefined reference to `TemplatedClass<int>::method()'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
[1/3] Building CXX object CMakeFiles/example.dir/other_library.cpp.o
[2/3] Building CXX object CMakeFiles/example.dir/main.cpp.o
[3/3] Linking CXX executable example

A results/executable symbols => results/executable symbols +46 -0
@@ 0,0 1,46 @@
$ nm --demangle ./build/example
0000000000004030 B __bss_start
                 w __cxa_finalize@GLIBC_2.2.5
0000000000004018 D __data_start
0000000000004018 W data_start
0000000000004020 D __dso_handle
0000000000004028 V DW.ref.__gxx_personality_v0
0000000000003db0 d _DYNAMIC
0000000000004030 D _edata
0000000000004038 B _end
00000000000012ec T _fini
0000000000003fe8 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000002034 r __GNU_EH_FRAME_HDR
                 U __gxx_personality_v0@CXXABI_1.3
0000000000001000 T _init
0000000000002000 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U __libc_start_main@GLIBC_2.34
0000000000001215 T main
                 U __stack_chk_fail@GLIBC_2.4
0000000000001060 T _start
0000000000004030 D __TMC_END__
                 U _Unwind_Resume@GCC_3.0
                 U operator delete(void*, unsigned long)@CXXABI_1.3.9
000000000000115a W ChildClass<int>::method()
00000000000012c2 W ChildClass<int>::ChildClass()
00000000000012c2 W ChildClass<int>::ChildClass()
00000000000011da W ChildClass<int>::~ChildClass()
00000000000011b0 W ChildClass<int>::~ChildClass()
00000000000011b0 W ChildClass<int>::~ChildClass()
0000000000001206 W TemplatedClass<int>::method()
00000000000012a8 W TemplatedClass<int>::TemplatedClass()
00000000000012a8 W TemplatedClass<int>::TemplatedClass()
0000000000001184 W TemplatedClass<int>::~TemplatedClass()
000000000000116a W TemplatedClass<int>::~TemplatedClass()
000000000000116a W TemplatedClass<int>::~TemplatedClass()
0000000000003d88 V typeinfo for ChildClass<int>
0000000000003da0 V typeinfo for TemplatedClass<int>
0000000000002010 V typeinfo name for ChildClass<int>
0000000000002020 V typeinfo name for TemplatedClass<int>
0000000000003d38 V vtable for ChildClass<int>
0000000000003d60 V vtable for TemplatedClass<int>
                 U vtable for __cxxabiv1::__class_type_info@CXXABI_1.3
                 U vtable for __cxxabiv1::__si_class_type_info@CXXABI_1.3

A results/exit code => results/exit code +1 -0
@@ 0,0 1,1 @@
1

M results/other_library.cpp.o symbols => results/other_library.cpp.o symbols +1 -1
@@ 5,7 5,7 @@ $ nm --demangle ./build/CMakeFiles/example.dir/other_library.cpp.o
0000000000000000 W ChildClass<int>::~ChildClass()
0000000000000000 W ChildClass<int>::~ChildClass()
0000000000000000 n ChildClass<int>::~ChildClass()
                 U TemplatedClass<int>::method()
0000000000000000 W TemplatedClass<int>::method()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()