Can't access static class variable in tests

asked 2016-11-16 10:11:05 -0600

kotoko gravatar image

Hello,

I'm trying to access a static class variable from the gtests but compiling yields a undefined reference error.

CMakeFiles/mapper2D-integration-test.dir/test/mapper2d_octree_integration.cpp.o: In function mapper::Mapper2DIntegrationTest_StaticFail_Test::TestBody()': mapper2d_octree_integration.cpp:(.text+0x22): undefined reference tomapper::Mapper2D::TEMP_MAX' collect2: error: ld returned 1 exit status

header file:

namespace mapper
{
class Mapper2D
{
public:
        static constexpr float TEMP_MAX;
}
}

mapper2d_octree_integration.cpp file:

#include <gtest/gtest.h>
namespace mapper {
class Mapper2DIntegrationTest : public ::testing::Test {
protected:
    Mapper2DIntegrationTest() 
    {
    }

    virtual ~Mapper2DIntegrationTest() {
    // You can do clean-up work that doesn't throw exceptions here.
    }
};

TEST_F(Mapper2DIntegrationTest, StaticFail) {
    EXPECT_EQ(Mapper2D::TEMP_MAX, 2.45f);
}
 }
 int main(int argc, char **argv){
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

MakeList

cmake_minimum_required(VERSION 2.8.3)
project(thrun)
add_definitions(-std=c++11 )
find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package( CATKIN_DEPENDS roscpp  )
include_directories(include ${catkin_INCLUDE_DIRS} )
add_library(mapper2d
  src/mapper2d.cpp
)
target_link_libraries(mapper2d
  ${catkin_LIBRARIES}
)

if (CATKIN_ENABLE_TESTING)
  get_filename_component(TEST_WITH_DATA_TEST_DIR "test/data" ABSOLUTE)
  catkin_add_gtest(mapper2D-integration-test 
    test/mapper2d_octree_integration.cpp
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test)
  target_link_libraries(mapper2D-integration-test ${catkin_LIBRARIES} mapper2d )

endif()

Why can't it see the variable and how can I fix it?

edit retag flag offensive close merge delete