c++ undefined reference to destructor

2019-06-28 07:29发布

问题:

Sorry about this, but I am re-opening this. After sorting the eigen errors, this cropped right back up again. Exactly the same code exactly the same error. (well, the compiler found the eigen headers this time.) So, same question:

I have searched for destructor c++ and undefined reference to no avail. However I am pretty sure this is a fairly simple slip on my part.

Error:

/tmp/ccDsaJ9v.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1

SASAGeometry.h:

class SASAGeometry
{
public:
//methods
  SASAGeometry() ;
  int makeFromFiles(char *, char *, char *) ;
  ~SASAGeometry() ;
//globals
  std::list<E......};

SASAGeometry.cpp

SASAGeometry::SASAGeometry(){}

int SASAGeometry::makeFromFiles(char * xyz_file, char * dat_file, char * atoms_file)
{
  sasa_transformMatrix basisMaker ;
  list<Vect...
...
}

SASAGeometry::~SASAGeometry(){}

geomTest.cpp

int main(int argv, char * argc[])
{
  list<Vector3d>::iterator listIterator ;
  char * xyz_file = argc[1] ;
  char * dat_file = argc[2] ;
  char * atoms_file = argc[3] ;
  SASAGeometry geomMaker ;
  int geomErr....

...
return 0 ;
}

makefile :

# compiler choice
CXX = g++

# executable path
BIN = .

# include paths (or lack thereof :p)
INCLUDE = -I .

# compilation flags
CXXFLAGS = -pipe # -O6

# linking flags
LFLAGS = -lm

# object declarations
GeomTest_OBJS = geomTest.o SASAGeometry.o

geomTest_source     = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp

SASAGeometry.o  :   SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h

geomTest.o  :   geomSetup.cpp SASAGeometry.o

# compile 
geomTest    : $(GeomTest_OBJS) makefile
            $(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
            $(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)

clean       : \rm *.o *~ p1

My INCLUDE and LIBS flags are all ok, all other methods in the SASAGeometry class are quite happily defined.

Thanks in advance.

回答1:

You are not including SASAGeometry.cpp in makefile along with geomTest.cpp. makefile should be something like this:

geomTest    : $(GeomTest_OBJS) makefile
            $(CC) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CFLAGS) $(geomTest_source) $(LFLAGS)
            $(CC) $(LIBS) $(INCLUDE) $(CFLAGS) -o $(BIN)/geomTest geomTest.o $(LFLAGS)