0

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.

  • 1
    I expect your problem is in your CMakeList.txt for your unit test. I use CMake but don't use googletest so I don't see how you are using the testing framework – drescherjm Oct 12 '20 at 15:32
  • 1
    `EXPECT_TRUE(formatter.process(8));` --> `EXPECT_TRUE(form.process(8));`? – cigien Oct 12 '20 at 15:35
  • yeah, that was a typo in the post, corrected it now. it's form.process(8) in the actual code. – Eamon Heaney Oct 12 '20 at 15:40
  • 1
    @EamonHeaney Can you add the exact output on the console please, including the lines before the linker error. IIRC there's a cmake option to get verbose output for diagnosis also. – πάντα ῥεῖ Oct 12 '20 at 15:51
  • 1
    Please, show complete code in form of [mcve]. Currently, it is unclear what `add_cell_module` command does, and what `core_TEST_SOURCES` property is intended to do. – Tsyvarev Oct 12 '20 at 15:54
  • apologies, I'm not a cmake expert and I thought those were universal commands. I'll update it. – Eamon Heaney Oct 12 '20 at 15:58
  • @πάνταῥεῖ added a fuller console output – Eamon Heaney Oct 12 '20 at 16:07
  • 1
    @EamonHeaney Not useful, I am looking for the compiler and linker commandlines specifically (not that I didn't mention that in my former comment already) – πάντα ῥεῖ Oct 12 '20 at 16:08
  • I can't copy-paste the output, so I've typed out a few of the lines right before the linker error. does that help? – Eamon Heaney Oct 12 '20 at 16:17

0 Answers0