Skip to content

Commit

Permalink
Adjust linker flags for gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Aug 11, 2024
1 parent 1f66493 commit e0c7b1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
13 changes: 12 additions & 1 deletion unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (MSVC)
set(BUILD_SHARED_LIBS OFF)
endif()

include(FetchContent)
FetchContent_Declare(
googletest
Expand All @@ -9,11 +13,18 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Prevent installing gtest/gmock
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
if (BUILD_SHARED_LIBS)
target_compile_definitions(gtest PRIVATE "GTEST_CREATE_SHARED_LIBRARY=1")
target_compile_definitions(gmock PRIVATE "GTEST_CREATE_SHARED_LIBRARY=1")
endif()

set(UNITTEST_BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
function(create_test target)
target_sources(${target} PRIVATE ${UNITTEST_BASE_DIR}/test_helper/test_main.cpp)
target_link_libraries(${target} gtest gmock)
if (BUILD_SHARED_LIBS)
set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
endif()
target_link_libraries(${target} gmock gtest)
add_test (NAME ${target} COMMAND $<TARGET_FILE:${target}>)
endfunction()

Expand Down
5 changes: 3 additions & 2 deletions unit_test/general/optimization_basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cstddef>
#include <memory>

#include "g2o/core/eigen_types.h"
#include "g2o/core/factory.h"
#include "g2o/core/hyper_graph.h"
#include "g2o/core/hyper_graph_action.h"
Expand Down Expand Up @@ -122,7 +123,7 @@ TEST(OptimizationBasics, ActionCalls) {
// Add vertices
for (size_t i = 0; i < kNumVertices; ++i) {
auto v = std::make_shared<MockVertexSE2>();
v->setEstimate(g2o::SE2());
v->setEstimate(g2o::SE2(g2o::Vector3::Random()));
v->setId(i);
v->setFixed(i == 0); // fix the first vertex
optimizer->addVertex(v);
Expand All @@ -133,7 +134,7 @@ TEST(OptimizationBasics, ActionCalls) {
auto e1 = std::make_shared<g2o::EdgeSE2>();
e1->vertices()[0] = optimizer->vertex((i + 0) % kNumVertices);
e1->vertices()[1] = optimizer->vertex((i + 1) % kNumVertices);
e1->setMeasurement(g2o::SE2(1., 0., 0.));
e1->setMeasurement(g2o::SE2(g2o::Vector3::Random()));
e1->setInformation(g2o::EdgeSE2::InformationType::Identity());
optimizer->addEdge(e1);
}
Expand Down
7 changes: 2 additions & 5 deletions unit_test/stuff/command_args_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,11 @@ class CommandArgsTestAdapter : public g2o::CommandArgs {
};
} // namespace

class CommandArgsTypeToStr : public testing::TestWithParam<int> {
protected:
CommandArgsTestAdapter command_args;
};
class CommandArgsTypeToStr : public testing::TestWithParam<int> {};

TEST_P(CommandArgsTypeToStr, type2str) {
const int type = GetParam();
const std::string type_str = command_args.type2str(type);
const std::string type_str = CommandArgsTestAdapter::type2str(type);
EXPECT_FALSE(type_str.empty());
}

Expand Down
3 changes: 3 additions & 0 deletions unit_test/test_helper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ add_library(unittest_helper ${G2O_LIB_TYPE}
utils.cpp
allocate_optimizer.cpp allocate_optimizer.h
)
if (BUILD_SHARED_LIBS)
set_target_properties(unittest_helper PROPERTIES COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
endif()
target_link_libraries(unittest_helper solver_eigen stuff gmock)

0 comments on commit e0c7b1a

Please sign in to comment.