Search code examples
c++cmakegoogletestundefined-referencefake-function-framework

Undefined reference to 'fff' at linker


I'm getting the following linker error when I build a GoogleTest executable (with CMake) that uses FFF (Fake Function Framework):

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/module_test.dir/objects.a(module_tes
ts.cpp.obj):module_tests.cpp:(.rdata$.refptr.fff[.refptr.fff]+0x0): undefined reference to `fff'

The project has a source file:

module_tests.cpp:

#include <gtest/gtest.h>


extern "C" {
#include "fff.h"
FAKE_VALUE_FUNC(uint16_t, MODULEA_getSampleValue);
}

extern "C" {
#include "module_b.h"
}

// Tests written here

Then the "module_b.h" itself #includes "module_a.h". I don't understand what is causing the undefined reference error. I'm building the project using Cmake, which I may have also configured incorrectly. The CMakeLists.txt for the tests is:

CMakeLists.txt:

add_executable(module_test
    src/module_tests.cpp
)

target_link_libraries(module_test
    PRIVATE
        gtest_main
        MODULE_A
        MODULE_B
    )

target_include_directories(module_test
    PUBLIC
        ../inc
)

include(GoogleTest)
gtest_discover_tests(module_test)

What do I have configured incorrectly here that would cause this undefined reference error?


Solution

  • You need to define the globals needed by FFF. If you look at the github page, or any of the tests included with FFF, you'll find that there is the line

    DEFINE_FFF_GLOBALS;
    

    whose expansion includes the definition of fff needed.

    fff_globals_t fff;
    

    This is taken literally from README.md. I don't see how you can possibly miss it.

    Here's how you would define a fake function for this in your test suite:

    // test.c(pp)
    
    #include "fff.h"
    DEFINE_FFF_GLOBALS;
    FAKE_VOID_FUNC(DISPLAY_init);