gcc: error: unrecognized option --wrap
Asked Answered
Q

1

7

[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?

Quanta answered 22/10, 2015 at 9:52 Comment(0)
S
5

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 ...

Shahjahanpur answered 22/10, 2015 at 11:5 Comment(4)
You sir, are a genius!! Btw, if I wanted a second, say function two, how would that look? gcc -Wl,--wrap=add --wrap=two or gcc -Wl,--wrap=add -Wl,--wrap=two?Quanta
You can pass multiple options with -Wl,<option1>,<option2>,..., so I'm guessing -Wl,--wrap=add,--wrap=twoShahjahanpur
you are correct. From #32709735 ... gcc -g -Wl,--wrap=something,--wrap=somethingElse code_new.c headers.c -l cmockaQuanta
A year later, and I also got it wokrking with two separate -Wl, wrap=, with no intervening comma. E.g. Wl,--wrap=something --wrap=somethingElseQuanta

© 2022 - 2024 — McMap. All rights reserved.