Search code examples
gcclinker-errorslinker-flags

gcc: error: unrecognized option --wrap


[Update] Sorry for top posting, but it might help to know this first, as it probably changes the problem.

The --wrap is an option to the linker ld, not to gcc.

But my Eclipse project is using gcc for the link stage.

How can I use wrap functions, as needed for cmocka unit testing?


I am using gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.

I am trying to use the linker option --wrap as specified here in order to use cmocka to unit test C code.

The error is

make all 
Building target: unit_test_C_code_example_project
Invoking: GCC C Linker
gcc --wrap=add -o "unit_test_C_code_example_project"  ./test_scripts/test_maths.o  ./software_under_test/mocks/mock_add.o  ./software_under_test/maths.o   -lcmocka
gcc: error: unrecognized option ‘--wrap=add’
make: *** [unit_test_C_code_example_project] Error 1

So, the linker is complaining about the --wrap=add option, but I don't know why.


Inevitably, if I don't post code, someone will request it, so here it is, even though I doubt that it is of relevance to a linker option problem:

In my unit test main, I have

static void test_multiply_two_by_three(void **state)
{
    will_return(__wrap_add(0, 2), 2);
    will_return(__wrap_add(2, 2), 4);
    will_return(__wrap_add(4, 2), 6);

    assert_int_equal(multiply(2, 3), 6);
}

In another file, I declare the mock function

int __wrap_add(int a, int b)
{
    return mock();
}

and, in my Software Under Test, the function to be wrapped is called

int multiply(int x, int y)
{
    int total = 0;
    int i;

    for (i = 0; i < y; i++)
        total += add(total, x);

    return total;
}

Please note that there is no function called add() anywhere in the build. What I am trying to achieve is to mock that function, using the __wrap_add linker option. Why does the linker reject it?


Solution

  • You can pass options to the linker with -Wl,<linker-option>. Alternatively there's -Xlinker <option> for some versions of gcc. Try gcc -Wl,--wrap=add ...