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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:37:46+00:00 2026-05-20T11:37:46+00:00

Currently, I have my makefile set up to compile and make a fairly large

  • 0

Currently, I have my makefile set up to compile and make a fairly large project. I have written a second cpp file with main function for running tests. I want these to run separately, but build together and they use the same files. How is this accomplished?

edit: As reference, here is my current makefile. I’m not sure how to adjust it.

CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp Foo.cpp Bar.cpp Test.cpp A.cpp B.cpp C.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=myprogram

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@
  • 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-20T11:37:46+00:00Added an answer on May 20, 2026 at 11:37 am

    Normally you would just have multiple targets and do something like this:

    .PHONY: all target tests
    
    all: target tests
    
    target: ...
        ...
    
    tests: ...
        ...
    

    Then you can just make (defaults to make all), or just make target or make tests as needed.

    So for your makefile example above you might want to have something like this:

    CC = g++
    CFLAGS = -c -Wall -DDEBUG -g
    LDFLAGS =
    COMMON_SOURCES = Foo.cpp Bar.cpp A.cpp B.cpp C.cpp
    TARGET_SOURCES = main.cpp
    TEST_SOURCES = test_main.cpp
    COMMON_OBJECTS = $(COMMON_SOURCES:.cpp=.o)
    TARGET_OBJECTS = $(TARGET_SOURCES:.cpp=.o)
    TEST_OBJECTS = $(TEST_SOURCES:.cpp=.o)
    EXECUTABLE = myprogram
    TEST_EXECUTABLE = mytestprogram
    
    .PHONY: all target tests
    
    all: target tests
    
    target: $(EXECUTABLE)
    
    tests: $(TEST_EXECUTABLE)
    
    $(EXECUTABLE): $(COMMON_OBJECTS) $(TARGET_OBJECTS)
        $(CC) $(LDFLAGS) $^ -o $@
    
    $(TEST_EXECUTABLE): $(COMMON_OBJECTS) $(TEST_OBJECTS)
        $(CC) $(LDFLAGS) $^ -o $@
    
    .cpp.o:
        $(CC) $(CFLAGS) $< -o $@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have one project that currently contains multiple packages. These packages make up
I currently have the following rule in a Makefile: ../obj/%.o: %.cpp mkdir -p ../obj/$<
I have written a make file. In the make file i have used a
I have a fairly large program written in C. It spans several files, and
Currently have password protection on my main and sub directories, however I'd like to
I am trying to write a make function to touch/create an empty file and/or
I have been trying to set up an EDE project for C++ (emacs24 +
I have a C++ eclipse project that I would like to easily compile In
So, to compile my executable, I need to have the library locations set up
I have 2 files. ./main.coffee and ./shared.coffee . I'd like to use makefile to

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.