1

I am using the Fake Function Framework to fake a function in a .c file and test it in my .cpp unit test file.

#include "..\fff.h"
extern "C"
{
    #include "ioDigitalInput.h"
}


DEFINE_FFF_GLOBALS;

FAKE_VALUE_FUNC(bool, ioFunc);

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTestfff
{
    TEST_CLASS(UnitTestfff)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {

        }
    };
}

The function ioFunc returns a bool and takes void as input.

When I try to build the code I get the following errors: "one or more multiply defined symbols found" and "ioFunc already defined in ioDigitalInput.obj"

What am I doing wrong?

Kasper
  • 43
  • 5

1 Answers1

1

The line

FAKE_VALUE_FUNC(bool, ioFunc);

in your .cpp unit test file defines the (fake) function ioFunc. The object file ioDigitalInput.obj also contains a (maybe not fake) definition of ioFunc, and you try to link this object with the compiled test, so ioFunc is multiply defined. Either don't try to link ioDigitalInput.obj, or mark the fake with the weak attribute as described in the fff README.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Thank you for your answer. Maybe I do not understand, I have added the link "#define FFF_GCC_FUNCTION_ATTRIBUTES __attribute__((weak))" just above the include "..\fff.h line but it tells me now that the "weak" identifier is undefined. – Kasper Nov 12 '21 at 08:57
  • Are you using the GCC? Perhaps see [GCC style weak linking in Visual Studio?](https://stackoverflow.com/questions/2290587/gcc-style-weak-linking-in-visual-studio) – Armali Nov 12 '21 at 09:26
  • I am using the default C++ compiler. I build the production code in a separate project within the same solution. I then use those library files in the test project to write unit test. – Kasper Nov 12 '21 at 10:06
  • I don't know _the default C++ compiler_, but it appears to me that the file _ioDigitalInput.obj_ is not used as a _library file_, wherefrom only not otherwise defined functions would be taken, but rather as a plain object file, wherefrom all the functions are taken. You may want to investigate how you can generate a true library file with your compilation system. – Armali Nov 12 '21 at 11:01
  • The default compiler in Visual Studio 2019 is MSVC – Kasper Nov 12 '21 at 11:40
  • 1
    I ended up splitting my project into multiple components instead of having one large project. This allowed me to fake off the dependencies with the fake function frame work. Thanks your for the help – Kasper Nov 17 '21 at 10:44