~williamvds/linking-by-example

d6cb4e380bd2b6f753a162e0195f8d112d26a1e0 — williamvds a month ago 343d424
Templated subclass attempt
12 files changed, 66 insertions(+), 52 deletions(-)

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

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

M library.cpp => library.cpp +1 -12
@@ 1,15 1,4 @@
#include "library.hpp"

template <typename T>
T square(T num) {
    return num * num;
}

template int square<>(int);
template long square<>(long);

int operand = 5;

int square_10() {
    return square(10);
}
int TemplatedClass<T>::method() { return 42; }

M library.hpp => library.hpp +6 -2
@@ 1,6 1,10 @@
#pragma once

template <typename T>
extern T square(T num);
class TemplatedClass
{
public:
    virtual ~TemplatedClass() = default;

extern int operand;
    virtual int method();
};

M main.cpp => main.cpp +2 -2
@@ 1,5 1,5 @@
#include "library.hpp"
#include "other_library.hpp"

int main() {
    return square(static_cast<long>(operand));
    return ChildClass<int>{}.method();
}

A other_library.cpp => other_library.cpp +7 -0
@@ 0,0 1,7 @@
#include "other_library.hpp"

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

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

A other_library.hpp => other_library.hpp +10 -0
@@ 0,0 1,10 @@
#pragma once

#include "library.hpp"

template <typename T>
class ChildClass : public TemplatedClass<T>
{
public:
    int method() override;
};

M results/build log => results/build log +9 -3
@@ 3,6 3,12 @@ $ 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/3] Building CXX object CMakeFiles/example.dir/main.cpp.o
[2/3] Building CXX object CMakeFiles/example.dir/library.cpp.o
[3/3] Linking CXX executable example
[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.

D results/executable symbols => results/executable symbols +0 -25
@@ 1,25 0,0 @@
$ nm --demangle ./build/example
0000000000004014 B __bss_start
                 w __cxa_finalize@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004000 W data_start
0000000000004008 D __dso_handle
0000000000003df0 d _DYNAMIC
0000000000004014 D _edata
0000000000004018 B _end
0000000000001160 T _fini
0000000000003fe8 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000002004 r __GNU_EH_FRAME_HDR
0000000000001000 T _init
0000000000002000 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U __libc_start_main@GLIBC_2.34
000000000000114a T main
0000000000004010 D operand
0000000000001020 T _start
0000000000004018 D __TMC_END__
0000000000001129 W int square<int>(int)
0000000000001138 W long square<long>(long)
0000000000001119 T square_10()

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

M results/library.cpp.o symbols => results/library.cpp.o symbols +0 -4
@@ 1,5 1,1 @@
$ nm --demangle ./build/CMakeFiles/example.dir/library.cpp.o
0000000000000000 D operand
0000000000000000 W int square<int>(int)
0000000000000000 W long square<long>(long)
0000000000000000 T square_10()

M results/main.cpp.o symbols => results/main.cpp.o symbols +29 -2
@@ 1,4 1,31 @@
$ nm --demangle ./build/CMakeFiles/example.dir/main.cpp.o
0000000000000000 V DW.ref.__gxx_personality_v0
                 U __gxx_personality_v0
0000000000000000 T main
                 U operand
                 U long square<long>(long)
                 U __stack_chk_fail
                 U _Unwind_Resume
                 U operator delete(void*, unsigned long)
                 U ChildClass<int>::method()
0000000000000000 W ChildClass<int>::ChildClass()
0000000000000000 W ChildClass<int>::ChildClass()
0000000000000000 n ChildClass<int>::ChildClass()
0000000000000000 W ChildClass<int>::~ChildClass()
0000000000000000 W ChildClass<int>::~ChildClass()
0000000000000000 W ChildClass<int>::~ChildClass()
0000000000000000 n ChildClass<int>::~ChildClass()
                 U TemplatedClass<int>::method()
0000000000000000 W TemplatedClass<int>::TemplatedClass()
0000000000000000 W TemplatedClass<int>::TemplatedClass()
0000000000000000 n TemplatedClass<int>::TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 n TemplatedClass<int>::~TemplatedClass()
0000000000000000 V typeinfo for ChildClass<int>
0000000000000000 V typeinfo for TemplatedClass<int>
0000000000000000 V typeinfo name for ChildClass<int>
0000000000000000 V typeinfo name for TemplatedClass<int>
0000000000000000 V vtable for ChildClass<int>
0000000000000000 V vtable for TemplatedClass<int>
                 U vtable for __cxxabiv1::__class_type_info
                 U vtable for __cxxabiv1::__si_class_type_info

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()
0000000000000000 W TemplatedClass<int>::method()
                 U TemplatedClass<int>::method()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()
0000000000000000 W TemplatedClass<int>::~TemplatedClass()