Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7187089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:46:10+00:00 2026-05-28T18:46:10+00:00

I would like to write tests for a C library, in C. I’d like

  • 0

I would like to write tests for a C library, in C. I’d like to mock out some functions for the test.

Suppose my library is compiled from the following source:

/* foo.h */
int myfunction(int x, int y);

/* foo.c */
#include "foo.h"

static int square(int x) { return x * x; }

int myfunction(int x, int y) {
    return square(x) + square(y);
}

I want to write a test like this:

/* foo_test.c */
#include "foo.h"

static int square(int x) { return x + 1; }

int main(void) {
    assert(myfunction(0, 0) == 2);
    return 0;
}

Is there any way I can compile so that myfunction will use the definition of square in foo_test.c, instead of the one in foo.c, only when linking the executable foo_test? That is, I want to compile foo.c into a library (let’s call it libfoo.so), and then compile foo_test.c with libfoo.so and some magic so that I’ll get an executable foo_test which uses the different implementation of square.

It would be helpful to hear solutions for when square is not declared static, but solving the above case would be even better.

EDIT: It seems hopeless, but here’s an idea: Suppose I compile with -O0 -g so it’s unlikely that square will get inlined and I should have symbols showing where the call was resolved. Is there a way to sneak into the object file and swap out the resolved reference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:46:11+00:00Added an answer on May 28, 2026 at 6:46 pm

    It looks like you are using GCC, so you can use the weak attribute:

    The weak attribute causes the declaration to be emitted as a weak
    symbol rather than a global. This is primarily useful in defining
    library functions which can be overridden in user code,
    though it can
    also be used with non-function declarations. Weak symbols are
    supported for ELF targets, and also for a.out targets when using the
    GNU assembler and linker.

    http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .NET file system wrapper library I would like to write a test
I would like to write some tests for the GUI of my Cocoa program.
I would like to write the unit test for a text parser class but
I would like to write some data to a file in Ruby. What is
I would like to write some scripts in python that do some automated changes
I have some needs, for a specific test type: I would like to check
I have a native library with some native ntype in it and would like
I would really like to write tests for Google App Engine using their python
I would like to write a small program in C# which goes through my
I would like to write a plug-in that will allow a custom written CRM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.