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

  • Home
  • SEARCH
  • 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 8664625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:17:06+00:00 2026-06-12T17:17:06+00:00

gcc 4.7.2 c89 Hello, I am wondering does any one know of any tutorials

  • 0
gcc 4.7.2
c89

Hello,

I am wondering does any one know of any tutorials or text books that cover using makefile to create some simple unit testing for my c programs.

I would like to run some automated testing that will create a test suite and add this to my Makefile.

Just want some ideas on how to get started.

Many thanks for any suggestions

  • 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-06-12T17:17:07+00:00Added an answer on June 12, 2026 at 5:17 pm

    Generating Test Function Snippets

    Generating function snippets is easy with the help of gccxml and nm. The following bash script generate_snippets.sh can be called with one command line argument to generate a test function snippet for each function defined in a source file:

    #!/bin/bash
    #
    # Generate stub functions for one file
    #
    
    # # Initialize
    FILE=$1
    
    [ ! -e "$FILE" ] && echo "file doesn't exist" && exit -1
    
    # # Compile
    OBJECT=$(mktemp)
    gcc -c $FILE -o $OBJECT
    
    # # Get Functions
    
    # ## Get all symbols in the text sections
    Y=$(mktemp)
    nm $OBJECT | grep " T " | awk '{print $3;}' | sort > $Y
    
    # ## Get functions defined in the file (including #includes)
    # get all functions defined in the compilation unit of $FILE, excluding included
    # dynamically linked functions
    
    XML=$(mktemp)
    X=$(mktemp)
    gccxml $FILE -fxml=$XML
    grep "<Function" $XML | sed 's/^.*name="\([^"]*\).*/\1/g' | sort > $X
    
    # ## get the common lines
    # This is done to get those functions which are defined in the source file and end up in
    # the compiled object file.
    COMMON=$(comm $Y $X -1 -2)
    
    # # Create stubs
    for func in $COMMON;
    do
        cat <<_
    // Test stub for $func. Returns 1 if it fails.
    char test_$func() {
        return 1;
    }
    _
    
    done
    
    # # Clean up
    
    rm $OBJECT $XML $X $Y
    

    TODOS

    The script is not yet perfect. You should probably include a test to only generate test functions for those functions which aren’t tested yet. As this is done analogous to finding the common names between $X and $Y, I leave this as an exercise. When this is implemented, it makes sense to run this script from a Makefile. See the other answer for pointers on that.

    Example Usage

    Consider the C file hello.c:

    #include <stdio.h>
    
    int foo();
    int bar();
    
    int main() {
        printf("hello, world\n");
        return 0;
    }
    
    
    int foo() {
        printf("nothing\n");
        return 1;
    }
    
    int bar() {
        printf("still nothing\n");
        return 1;
    }
    

    Running the script above with this file as input yields the following output:

    // Test stub for bar. Returns 1 if it fails.
    char test_bar() {
        return 1;
    }
    // Test stub for foo. Returns 1 if it fails.
    char test_foo() {
        return 1;
    }
    // Test stub for main. Returns 1 if it fails.
    char test_main() {
        return 1;
    }
    

    Just put those snippets into the appropriate file and fill them with logic as needed. After that, compile the test suite and run it.

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

Sidebar

Related Questions

gcc (GCC) 4.7.0 c89 x86_64 Hello, I am wondering is it worth it using
gcc 4.4.4 c89 I am just wondering is there any standard that should be
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am wondering if I
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
gcc (GCC) 4.7.0 c89 Hello, I am wondering if I am thinking correctly here.
gcc (GCC) 4.7.0 c89 Hello, I have the following structure that I am trying
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am creating a thread
gcc (GCC) 4.6.3 c89 apache runtime portable libraries Hello, Just a simple question I
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make

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.