1

I've encountered a weird problem. My code works perfectly in Release build, but fails to compile in Debug. Quite the opposite to the problems one usually have. :)

in *.h file I have a constant:

class OBDenrChrg : public OBChargeModel
{
public:
  bool ComputeCharges(OBMol &mol);
// blah-blah-blah
private:
  static const float c = 0.05;
};

in *.cpp file I use this constant:

bool OBDenrChrg::ComputeCharges(OBMol &mol)
{
// blah-blah-blah
  Eigen::VectorXf a0 = L*EN*c;
  Eigen::SparseMatrix<float> A = L*HRD*c + I;
// blah-blah-blah
}

The built is controlled by cmake. The Release target builds and works well. The Debug says:

CMakeFiles/plugin_charges.dir/charges/denr.o: 
In function `OpenBabel::OBDenrChrg::ComputeCharges(OpenBabel::OBMol&)':
/home/titov/openbabel-2.3.2/src/charges/denr.cpp:181: undefined reference to `OpenBabel::OBDenrChrg::c'
/home/titov/openbabel-2.3.2/src/charges/denr.cpp:182: undefined reference to `OpenBabel::OBDenrChrg::c'

I have no #ifdef DEBUG or similar switches in these files.

Any ideas on how to fix the problem and what may cause it?

Edit:

Thanks everyone for the help. The problem was in the abscence of c's definition, since declaration of a static member inside of class' definition does not define the static member. What a mess...

Oleg Titov
  • 1,100
  • 1
  • 8
  • 13
  • 3
    My guess? The release build optimizes out the use of the variable (i.e. the compiler replaces all instances of `c` with its value) so it's actually not referenced anywhere in the generated code. The debug build does not do that optimization, and so you need a *definition* of the variable. There are plenty of duplicates all over the Internet on how to define a `static` member variables, including quite a few here on Stack Overflow. – Some programmer dude Sep 25 '16 at 12:43
  • Where have you put the definition of `OBDenrChrg::c`? (I suspect an ODR violation which wouldn't _require_ a diagnostic.) – CB Bailey Sep 25 '16 at 12:44

0 Answers0