使用gtest(C ++)时,链接器无法找到对象引用

时间:2018-11-21 05:42:14

标签: c++ unit-testing cmake linker googletest

我决定开始进行单元测试,并使用gtest编写了我的第一个单元测试。来源:

DetectorTests.cpp:

const path = require('path');
const merge = require('lodash.merge');
const glob = require('glob')
const Uglify = require('uglifyjs-webpack-plugin');
module.exports = {
  webpack: (config) => {
    config.resolve = merge({}, config.resolve, {
      alias: {
        Components: path.resolve(__dirname, 'components'),
        Libs: path.resolve(__dirname, 'libs'),
        Sagas: path.resolve(__dirname, 'sagas'),
      },
    });


    config.module.rules.push(
      {
        test: /\.(css|scss)/,
        loader: 'emit-file-loader',
        options: {
          name: 'dist/[path][name].[ext]'
        }
      }
    ,
      {
        test: /\.css$/,
        use: ['babel-loader', 'raw-loader', 'postcss-loader']
      }
      ,
      {
        test: /\.css$/,
        loaders: [
          'style-loader',
          'css-loader'
        ]
      },  
      {
        test: /\.s(a|c)ss$/,
        use: ['babel-loader', 'raw-loader', 'postcss-loader',
          { loader: 'sass-loader',
            options: {
              includePaths: ['scss', 'node_modules','styles']
                .map((d) => path.join(__dirname, d))
                .map((g) => glob.sync(g))
                .reduce((a, c) => a.concat(c), [])
            }
          }
        ]
      }
    )
    config.node = {
      fs: 'empty',
    };
    return config;
  },
};

我的CMakeLists.txt具有以下几行:

#include <gtest/gtest.h>

#include <ros_layer/utils/DetectorUtils.h>



class DetectorTests : public ::testing::Test {
 public:
  DetectorTests() {
  }

  bool initTestFixture() {
    return true;
  }

  virtual void SetUp() {
    ASSERT_TRUE(initTestFixture());
  }

  virtual void TearDown() {
  }

};

// Test where the robot does a vector operation
TEST_F(DetectorTests, testNoDetect) {
  // Parameters get set up here
  ros::NodeHandle privateNodeHandle("~");
  std::shared_ptr<ros_layer::DetectorUtils> distDetector = std::make_shared<ros_layer:DetectorUtils>(privateNodeHandle);

  // @to-do set up object parameters before running test
  ASSERT_FALSE(distDetector->checkReadings());
}

int main(int argc, char** argv) {

  ros::init(argc, argv, "detection_action_test");
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

当我使用带有运行测试的参数的CMake运行时,在链接时出现此错误:

find_package(GTest REQUIRED)
catkin_add_gtest(detector-test test/DetectorTests.cpp)
target_link_libraries(detector-test ${catkin_LIBRARIES})

如果我删除了所有与单元测试有关的代码,只用创建了DetectorUtils对象的方式编写了c ++,我一点也不麻烦。是什么导致此错误,我该如何解决?

1 个答案:

答案 0 :(得分:0)

解决了。我的CMakeList中的target_link_libraries需要具有定义DetectorUtils的库。这是更新的行:

target_link_libraries(dragging-test ROSLayerLibrary ${catkin_LIBRARIES})