質問

I'm trying to write a class that needs to deallocate some memory, so I've defined a custom destructor. This is compiled as a shared library. However, when I try to compile a basic program to use that library, I'm getting the usual "undefined reference" error when a definition cannot be found. If I remove the destructor, this does not occur.

Here's a stripped-down example:

Header file:

#ifndef _SKYMAP_H_
#define _SKYMAP_H_

#include <vector>
#include "TCanvas.h"

class BL_Skymap {
 public:
   BL_Skymap();
   ~BL_Skymap();

 protected:
   TCanvas mCanvas;
};

#endif //_BENSLIBRARY_SKYMAP_H_

Source file:

\#include "BL_Skymap.h"

BL_Skymap::BL_Skymap()
{
}

BL_Skymap::~BL_Skymap()
{
}

Now the program I'm using is simply this:

\#include "BL_Skymap.h"

int main()
{
  BL_Skymap map;
  return(0);
}

Note that I'm using the ROOT analysis package (that's the TCanvas object). When I compile the small program above, I get the following errors (the Skymap class is compiled into libMyLibrary.so):

g++ test.cpp -o test -lMyLibrary `root-config --cflags --glibs`
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libMyLibrary.so: undefined reference to 'TCanvas::~TCanvas()'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libMyLibrary.so: undefined reference to 'TCanvas::TCanvas(bool)'

Please note that the root package gives a utility to generate the required compiler flags, which is the purpose of root-config --cflags --glibs above. What am I missing here?

UPDATE: I've written a Makefile to perform the compilation of my library, which executes the following:

g++ -Wall -Wextra -ansi -pedantic --std=c++11 -Isrc -Ihdr -MM -MT 'obj/BL_Skymap.o' src/BL_Skymap.cpp -MF BL_Skymap.d `root-config --cflags --glibs`
g++ -Wall -Wextra -ansi -pedantic --std=c++11 -Isrc -Ihdr -fPIC -o obj/BL_Skymap.o -c src/BL_Skymap.cpp `root-config --cflags --glibs`
g++ -Wall -Wextra -ansi -pedantic --std=c++11 -shared obj/*.o -o libMyLibrary.so

UPDATE2: I found the problem - in the final step in the compilation above, I was forgetting to add the call to root-config and therefore libMyLibrary.so wasn't linking to the ROOT libraries like it should have.

役に立ちましたか?

解決

As stated in an update to the original question, my problem was that I was using the relevant flags for the ROOT libraries when I was compiling the BL_Skymap.o object file, but not when I was linking the object file together to make the libMyLibrary.so file.

Adding the ROOT flags to this final step fixed the problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top