0

Trying to understand what's going on with symbol table when class constructor/destructor is inlined vs implemented in .cpp file. Here's the code:

class.hpp

class Class
{
public:
#ifdef CUSTOM_CTOR
    #ifdef HEADER_IMPL
        Class(){ }
        virtual ~Class(){}
    #else
        Class();
        virtual ~Class();
    #endif
#endif

private:
void init();
};

class.cpp

#include "class.hpp"

#ifdef CUSTOM_CTOR
#ifndef HEADER_IMPL
Class::Class () {}
Class::~Class() {}
#endif
#endif

void Class::init(){ }

I compile code into object file and then inspect it with nm. Here are the results:

$ clang++ class.cpp -c -DCUSTOM_CTOR -DHEADER_IMPL -o class.o
$ nm class.o
0000000000000000 T __ZN5Class4initEv
$ clang++ class.cpp -c -DCUSTOM_CTOR -o class.o
$ nm class.o 
00000000000000cc s GCC_except_table4
                 U __Unwind_Resume
00000000000000c0 T __ZN5Class4initEv
0000000000000020 T __ZN5ClassC1Ev
0000000000000000 T __ZN5ClassC2Ev
0000000000000070 T __ZN5ClassD0Ev
0000000000000050 T __ZN5ClassD1Ev
0000000000000040 T __ZN5ClassD2Ev
00000000000000f8 S __ZTI5Class
0000000000000108 S __ZTS5Class
00000000000000d8 S __ZTV5Class
                 U __ZTVN10__cxxabiv117__class_type_infoE
                 U __ZdlPv
                 U ___gxx_personality_v0

Symbol table for the code when constructor and destructor are inlined doesn't have any typeinfo nor vtable. Why is this happening (macOS platform)?

(also, compiling with default construtor/destructor yields results as in the case with inlined constructor).

peetonn
  • 2,942
  • 4
  • 32
  • 49
  • Evil code. Class and structure names should vary by more than capitalization. The `class Class` violates a lot of coding guidelines. – Thomas Matthews Jan 30 '20 at 00:56
  • Are `CUSTOM_CTOR` and `HEADER_IMPL` defined when you compiled? Which were defined? – Thomas Matthews Jan 30 '20 at 00:59
  • @ThomasMatthews See the flags given to the compiler. – L. F. Jan 30 '20 at 02:02
  • Hmm... Few programmers take the time to look at what is in their object files. Consequentially, most programmers would not notice this result until the linker complains that the vtable is not found. So it might be reasonable to consider this a duplicate of [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable). At least the highest-voted answer seems applicable. (I have an answer in there as well that is a bit more verbose, but it's further down the list.) By the way, the version of your compiler might be relevant. – JaMiT Jan 30 '20 at 03:27
  • 1
    Does this answer your question? [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) – JaMiT Jan 30 '20 at 03:27
  • @ThomasMatthews the code is purely for the example. it was a different class name before i did search&replace to paste it here – peetonn Jan 31 '20 at 01:19

0 Answers0