public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How can I get the linker to use the correct function
@ 2024-04-18 19:20 Chris Abbott
  0 siblings, 0 replies; only message in thread
From: Chris Abbott @ 2024-04-18 19:20 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1530 bytes --]

Context: unit testing with ceedling
System: Windows PC, ceddling builds to .exe file
Problem: how to stub a function "MyFunc()" in the same file.c your testing

Solution:

1.       Create a duplicate function in my test_file.c

2.       Use -allow-multiple-definition

3.       First instance of function should be taken (test_file.o due to linking order) - Therefore overriding the original

Problem:

-          MAP file shows "MyFunc()" is being used from test_file.c => GOOD

-          EXE file has both versions of "MyFunc()" and is executing the one from "file.c" => BAD

Questions:

a)       Why does the MAP file not match the EXE

b)      Are there any compiler/linker flags I can use to resolve this problem


Source code:
file.c
-------------------------------------------------
#include <stdio.h>

int MyFunc(void)
{
    return 3;
}

int MyApplication(void)
{
    if ( MyFunc() == 3 )
        return 7;
    else
        return 12;
}

test_file.c
-------------------------------------------------
#include "unity.h"

extern int MyApplication(void);

int myfunc_return_value = 0;

int MyFunc(void)  // stub replacing real function
{
    return myfunc_return_value;
}

int test_MyApplication(void)
{
    int result = 0;

    myfunc_return_value = 3;
    result = MyApplication()
    TEST_ASSERT_EQUAL_INT(7, result)

    myfunc_return_value = 1;
    result = MyApplication()
    TEST_ASSERT_EQUAL_INT(12, result)
}


Chris Abbott
Firmware Engineer

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-18 19:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 19:20 How can I get the linker to use the correct function Chris Abbott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).