0

I have one "helper" file included in two "main" files which are built into two executables with the same makefile. I have debug print statements in the helper file. I want the print statements to actually be printed in one executable, but not the other. Is there a way to do it? Right now I have the following, and I was hoping to compile with DEBUG_PRINT defined for one executable but not the other, but I don't see how.

main1.cpp:

    #include "helper.h"
    ...

main2.cpp:

    #include "helper.h"
    ...

helper.cpp:

    #ifdef DEBUG_PRINT
    cout << "here is a debug message" << endl;
    #endif

Makefile:

    build: main1 main2
    main1: main1.o helper.o
        g++ -g -o main1 main1.o helper.o
    main2: main2.o helper.o
        g++ -g -o main2 main2.o helper.o
    %.o: %.cpp
        gcc -g -c $<

1 Answer 1

4

You will need two different object files (main1-helper.o and main2-helper.o) and target-specific compiler flags:

 main1: CXXFLAGS=-DDEBUG_PRINT
 %.o: %.cpp
      gcc $(CXXFLAGS) -g -o $@ -c $<

Note: This leaves you with the problem of generating main1-helper.o from helper.o. There are a few ways to solve this; however, you might be more comfortable using automake from the start instead of rolling your own solutions.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I misread the Makefile at first. There's no way around having two different object files aside from moving $(CXXFLAGS) helper.cpp to the g++ link command (which seems kind of dirty).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.