代码之家  ›  专栏  ›  技术社区  ›  upsidedownone

链接器处对“fff”的未定义引用

  •  -1
  • upsidedownone  · 技术社区  · 1 年前

    当我构建一个使用FFF(Fake Function Framework)的GoogleTest可执行文件(使用CMake)时,我得到了以下链接器错误:

    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'
    

    该项目有一个源文件:

    模块测试应用程序:

    #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
    

    那么“module_b.h”本身#包括“module_a.h”。我不明白是什么导致了未定义的引用错误。我正在使用Cmake构建项目,我可能也配置不正确。测试的CMakeLists.txt是:

    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)
    

    我在这里错误地配置了什么,会导致这个未定义的引用错误?

    1 回复  |  直到 1 年前
        1
  •  2
  •   Weijun Zhou    1 年前

    您需要定义FFF所需的全局变量。如果你查看github页面,或者FFF中包含的任何测试,你会发现

    DEFINE_FFF_GLOBALS;
    

    其扩展包括的定义 fff 需要。

    fff_globals_t fff;
    

    这是从字面上取自 README.md 。我看你怎么会错过它。

    以下是如何在测试中为此定义一个伪函数 一套:

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