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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:45:29+00:00 2026-06-13T01:45:29+00:00

Here’s the use case: I have a .cpp file which has functions implemented in

  • 0

Here’s the use case:

I have a .cpp file which has functions implemented in it. For sake of example say it has the following:

[main.cpp]

#include <iostream>

int foo(int);

int foo(int a) {
    return a * a;
}

int main() {
    for (int i = 0; i < 5; i += 1) {
        std::cout << foo(i) << std::endl;
    }

    return 0;
}

I want to perform some amount of automated testing on the function foo in this file but would need to replace out the main() function to do my testing. Preferably I’d like to have a separate file like this that I could link in over top of that one:

[mymain.cpp]

#include <iostream>
#include <cassert>

extern int foo(int);

int main() {
    assert(foo(1) == 1);
    assert(foo(2) == 4);
    assert(foo(0) == 0);
    assert(foo(-2) == 4);        

    return 0;
}

I’d like (if at all possible) to avoid changing the original .cpp file in order to do this — though this would be my approach if this is not possible:

  1. do a replace for "(\s)main\s*\(" ==> "\1__oldmain\("
  2. compile as usual.

The environment I am targeting is a linux environment with g++.

  • 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-13T01:45:30+00:00Added an answer on June 13, 2026 at 1:45 am

    I hate answering my own question, but here’s a solution I ended up finding deep in the man page of g++, I’ve tested it and it works to what I would want it to…

    g++ has the -D flag which allows you to define macros when compiling object files. I know you are thinking “ugh macros” but hear me out… You can use the macro definition to effectively rename a symbol. In my case, I can run the following command to generate an object file of my students code without their main file: g++ -D main=__students_main__ main.cpp -c -o main.nomain.o.

    This creates an object file with their int main defined as int __students_main__. Now this isn’t necessarily callable directly as they could have defined main as int main(void) or with the various combinations of argc and argv, but it allows me to effectively compile out their function.

    The final compile looks like this:

    g++ -c -D main=__students_main__ main.cpp -o main.nomain.o
    g++ -c mymain.cpp -o mymain.o
    g++ main.nomain.o mymain.o -o mymainstudentsfoo.out
    

    For my purposes, I wanted to create a Makefile that would accomplish this automagically (ish) and I feel that is relevant to this discussion so I’ll post what I came up with:

    HDIR=./ # Not relevant to question, but we have headers in a separate directory
    CC=g++
    CFLAGS=-I $(HDIR)
    NOMAIN=-D main=__student_main__ # The main renaming magic
    
    .SECONDARY: # I forget exactly what this does, I seem to remember it is a hack to prevent deletion of .o files
    
    cpp = $(wildcard *.cpp)
    obj = $(patsubst %.cpp,%.o,$(cpp))
    objnomain = $(patsubst %.cpp,%.nomain.o,$(cpp))
    
    all: $(obj) $(objnomain)
    
    clean:
            rm -f *.o *.out
    
    %.nomain.o: %.cpp
            $(CC) $(CFLAGS) $(NOMAIN) -c $^ -o $@
    
    %.o: %.cpp
            $(CC) $(CFLAGS) -c $^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are the tables I have: Table A which has entries with item and
Here is an example: I have the generic type called Account. I wish to
Here a simple question : What do you think of code which use try
Here i have gridview which contain some values retrieved from database.We put radiobutton list
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and
Here is my problem : I have a hierarchy of subfolders that each contain
Here is my problem: Using preg_replace('@\b(word)\b@','****',$text); Where in text I have word\word and word
Here is my scenario: I have one layout with a view where I can

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.