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 3669346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:13:55+00:00 2026-05-19T02:13:55+00:00

I want to be able to use Cmockery to mock C functions called from

  • 0

I want to be able to use Cmockery to mock C functions called from C++ code I’m testing. As a step towards that, I’ve renamed the Cmockery example run_tests.c to run_tests.cpp, and am attempting to compile and link it with cmockery.c:

g++ -m32 -DHAVE_CONFIG_H -DPIC -I ../cmockery-0.1.2 -I /usr/include/malloc -c run_tests.cpp -o obj/run_tests.o
gcc -m32 -DHAVE_CONFIG_H -DPIC -Wno-format -I ../cmockery-0.1.2 -I /usr/include/malloc -c ../cmockery-0.1.2/cmockery.c -o obj/cmockery.o
g++  -m32 -o run_tests obj/run_tests.o obj/cmockery.o

The first two command lines (to compile) are successful, but after the last I get:

Undefined symbols:
  "_run_tests(UnitTest const*, unsigned long)", referenced from:
      _main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

That undefined symbol is from line 29 of run_tests.cpp:

return run_tests(tests);

The run_tests() function is defined in cmockery.c.

After reading “Linking C++ code with ‘gcc’ (without g++)“, I tried:

gcc -lstdc++ -m32 -o run_tests obj/run_tests.o obj/cmockery.o

But got the same result:

Undefined symbols:
  "_run_tests(UnitTest const*, unsigned long)", referenced from:
      _main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

How do I compile and link C++ code so it finds the symbols in C code?

  • 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-19T02:13:56+00:00Added an answer on May 19, 2026 at 2:13 am

    I think that you can get thinkgs to link from C++ by adding the following around the contents of the cmockery.h file:

    At or near the beginning:

    #if defined(__cplusplus)
    extern "C" {
    #endif
    

    At or near the end:

    #if defined(__cplusplus)
    }
    #endif
    

    That way, use of the header in C sources will ignore the extern "C" part of the declaration, but when the header is include in C++ builds, the compiler will be properly told that the linkage for the declarations in that header use C semantics.

    For a quick-n-dirty test or if you’d rather not modify the header, you can try:

    extern "C" {
    #include "cmockery.h"
    }
    

    but my preference would be to put the extern "C" block in the header (and only around the stuff that’s required – that might need a bit of analysis).

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

Sidebar

Related Questions

I have an application that I want to be able to use large numbers
I want to be able to use in my code the following construct: p
I want to be able to use the abort function from pylons.controllers.util to display
I have a method called revisions, and I want to be able use the
I want to be able to use a varable table to delete records from
I want to be able to use items from database to generate some routes.
I want to be able to use math functions on my vector3Ds like so
I've got an app that I want to be able to use Custom URL
I want to be able to use methods that I haven't required() at the
I want to be able to use one of CORBA functionalities, that is I

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.