I've got a class defined in a .hpp that works fine, but when I try to move the function definitions to a .cpp the compiler throws a linker error in the unittests.
formatter.hpp:
#ifndef EXAMPLE_FORMATTER_HPP
#define EXAMPLE_FORMATTER_HPP
namespace formatters {
class Formatter
{
public:
bool process(int i);
};
}
#endif
formatter.cpp:
#include "formatter.hpp"
namespace formatters {
bool Formatter::process(int i)
{
...
}
}
CMakeLists.txt:
set_property(GLOBAL APPEND PROPERTY formatter_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/formatter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/formatter.hpp
)
get_property(formatter_sources GLOBAL PROPERTY formatter_SOURCES)
add_cell_module(formatter
${formatter_sources})
unittests/formattertests.cpp
#include "formatters/formatter.hpp"
#include <gtest/gtest.h>
TEST(FormatterTests, TestProcess)
{
formatter::Formatter form;
EXPECT_TRUE(form.process(8)); //linker error here,
//"undefined reference to formatter::Formatter::process(int)"
}
What am I doing wrong here? I've been playing spot-the-difference with other working examples within this project, but I can't see anything that I've been doing differently.
EDIT: I've read the other questions on here about linker errors. If they answered my question, I wouldn't be posting one of my own.
EDIT2: Here is my unittests/CMakeLists.txt file:
if(CORE_UNITTESTS)
set_property(GLOBAL APPEND PROPERTY core_TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/formattertests.cpp
)
endif()
It's essentially the same as the CMakeLists.txt files for all the other unittest directories in the project, and they work fine.
EDIT3: More complete console output:
[ 79%] Building CXX object CMakeFiles/core0unittests.dir/src/.../proto/fillprototests.cpp.o
[ 79%] Linking CXX executable core-unittests
/opt/.../gcc-9.3.0/bin/../lib/gcc/.../x86_64-pc-linux-gnu/bin/ld: CMakeFiles/core-unittests.dir/src/.../formatters/unittests/formattertests.cpp.o: in function 'FormatterTests_TestProcess_test::TestBody()':
/home/.../formatters/unittests/formattertests.cpp:7: undefined reference to 'formatter::Formatter::process(int)'
collect2: error: ld returned 1 status
CMakeFiles/core-unittests.dir/build.make:1896: recipe for target 'core-unittests' failed
...
EDIT4: It's clear to me at this point that I need to read up more on cmake to figure out what the error is. Thanks for all the help.