Defining namespace in g++ works for some files but fails for others

I won’t be able to show any code, but let me explain what is happening:

I’m attempting to compile some software with g++; I have my Makefile setup. There is a main file which calls the necessary functions to get this software working. I have all of my dependencies includes, i.e. all of the header files, all of the .cpp sources, etc.

My problem is, several of the files require me to define a namespace called OPTLEVEL or else g++ errors out.

When I do this, for example: -DOPTELEVEL=GENERIC, those files then no longer error out since I now defined OPTLEVEL; however, several of the other .cpp files now error out because I defined OPTLEVEL. This is all a conundrum and I am not sure what to do.

Here is an example Makefile with how I’m compiling the source files separately; if I were to combine them together in one line with the Define, it would give me an error. This gives me undefined references as I explain below:

HEADERS_NO_DEFINE = /Header/No/Define/Path
SOURCES_DEFINE_NEEDED = define1.cpp define2.cpp
SOURCES_NO_DEFINE = noDefine1.cpp noDefine2.cpp

    g++ *.o -o out

I have attempted to compile the .cpp files which require the define separately into their object files. Then I compiled the .cpp files which do not require the defines into their own separate object files, but linking them all together gives undefined references understandably.

The undefined references specifically appear due to the files needed the define now have their namespace looking like GENERIC::FUNCTION_NAME and the ones without the defines looking like OPTLEVEL:FUNCTION_NAME.

I’ll take any sort of advice.

Let me know if this doesn’t make sense, and I’ll attempt to clarify.


EDIT: Here is an example with what I’m looking at: define1.cpp

#ifndef OPTLEVEL
#error "Did not define OPTLEVEL 

namespace OPTLEVEL {
    class SampleClass : public InheritedClass {


The solution to my original question was to compile the source files that needed OPTLEVEL defined into their own libraries. Then when it came to compiling the main.cpp file, I had to ensure I linked those .a’s, and added the linking flags i.e. -llibName.

It took a lot of effort, but this is exactly what needs to be done.