Unfortunately this does work. GCC compiles this source without any error, but linker ends with error: undefined reference to MyTest::testVal.
Does not anybody know, whats wrong here? CPPUNIT_ASSERT(testVal == obj.getValue()) works fine, or there is another workaround using local variable within the testSomething() method. But why it is not possible to use CPPUNIT_ASSERT_EQUAL()?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm not entirely sure why you get the linker error, but I have a few questions to help figure it out:
Can you describe the file structure of the code you use here? What file and directory is this MyTest class in and where is the class for the obj variable referenced in your code snippet?
Also how are you compiling and linking the files in this example project? Can you give the exact command-line you are using?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You may be running into a situation where the compiler 'optimized' testVal out of existence. Try building with no optimizations (i.e. -O0 ) capital letter O followed by a zero.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I encounter following problem: Within a test method I want to use CPPUNIT_ASSERT_EQUAL macro and pass a static const to it as expected parameter:
class MyTest : public CppUnit::TestFixture {
static const int testVal = 123;
void testSomething() {
// ...
CPPUNIT_ASSERT_EQUAL(testVal, obj.getValue());
}
}
Unfortunately this does work. GCC compiles this source without any error, but linker ends with error: undefined reference to MyTest::testVal.
Does not anybody know, whats wrong here? CPPUNIT_ASSERT(testVal == obj.getValue()) works fine, or there is another workaround using local variable within the testSomething() method. But why it is not possible to use CPPUNIT_ASSERT_EQUAL()?
I'm not entirely sure why you get the linker error, but I have a few questions to help figure it out:
Can you describe the file structure of the code you use here? What file and directory is this MyTest class in and where is the class for the obj variable referenced in your code snippet?
Also how are you compiling and linking the files in this example project? Can you give the exact command-line you are using?
File test.cc:
class MyTest ... {
...
};
const int MyTest::testVal; // missing
> nm -C test|grep testVal
... R MyTest::testVal
You may be running into a situation where the compiler 'optimized' testVal out of existence. Try building with no optimizations (i.e. -O0 ) capital letter O followed by a zero.