public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Chris Abbott <Chris.Abbott@toyotatmh.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: How can I get the linker to use the correct function
Date: Thu, 18 Apr 2024 19:20:54 +0000	[thread overview]
Message-ID: <DS0PR11MB7957C7D0582958FEB9D2BEDD9F0E2@DS0PR11MB7957.namprd11.prod.outlook.com> (raw)

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

                 reply	other threads:[~2024-04-18 19:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DS0PR11MB7957C7D0582958FEB9D2BEDD9F0E2@DS0PR11MB7957.namprd11.prod.outlook.com \
    --to=chris.abbott@toyotatmh.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).