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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:06:15+00:00 2026-05-15T23:06:15+00:00

I have recently begun writing unit tests (using GoogleTest ) for a C++ project.

  • 0

I have recently begun writing unit tests (using GoogleTest) for a C++ project. Building the main project is fairly simple: I use GCC’s -MM and -MD flags to automatically generate dependencies for my object files, then I link all of the object files together for the output executable. No surpirses.

But as I’m writing unit tests, is there a way to have make or GCC figure out which object files are needed to compile each test? Right now, I have a fairly naive solution (if you can call it that) which compiles ALL available object files together for EVERY unit test, which is obviously wasteful (in terms of both time and space). Is there a way (with make, gcc, sed, or whatever else) to divine which object files are needed for a given unit test in a fashion similar to how dependencies are generated for the original source files?

  • 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-15T23:06:16+00:00Added an answer on May 15, 2026 at 11:06 pm

    It sounds like you have two groups of source files: one that actually implements your program, and another that’s all the unit tests. I assume each unit test has its own main function, and unit tests never need to call each other.

    If all that’s true, you can put all the files from the first group in a static library, and link each of the unit tests against that library. The linker will automatically pull from the library only the object files that are needed.

    In concrete Makefile terms:

    LIBRARY_OBJECTS = a.o b.o c.o d.o # etc
    UNIT_TESTS = u1 u2 u3 u4 # etc
    UNIT_TEST_OBJECTS = $(UNIT_TESTS:=.o)
    
    libprogram.a: $(LIBRARY_OBJECTS)
            ar cr $@ $?
    
    $(UNIT_TESTS): %: %.o libprogram.a
            $(CC) $(CFLAGS) -o $@ $< -lprogram
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently began using BIRT and have developed a report to use with my
I've recently become interested in algorithms and have begun exploring them by writing a
I have recently begun the practice of using <%# Ruby on Rails comments %>
I have recently begun learning the Win32 API using this tutorial: http://www.winprog.org/tutorial/ (though I'm
I've recently began using dTrace and have noticed just how awesome it is. Its
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently started using Vim as my text editor and am currently working
Recently, I've begun writing my own PHP OpenID consumer class in order to better
I recently began profiling an osgi java application that I am writing using VisualVM.
Two questions about unit tests. I've been writing unit tests for a while, however

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.