sunmat
sunmat

Reputation: 7268

Disabling the effect of _GLIBCXX_DEBUG for a specific variable

If a C++ program is compiled with -D_GLIBCXX_DEBUG, is there a way to specify in its source code that for a specific variable of type std::vector I DO NOT want the "debug" version of this class but the "normal" version instead?

My use-case is the following:

I have a C library (which I will call libA) and a C++ library (which I will call libB) presenting a C interface. Those libraries haven't been compiled with -D_GLIBCXX_DEBUG. The main program (which is compiled with -D_GLIBCXX_DEBUG) must call a function of libA and pass it an std::vector<char>* converted into a void*. libA calls a libB function that is given the void* and casts it back into an std::vector<char>* to use it (oh the joy of passing C++ data through a C layer...). Since the main program is compiled with -D_GLIBCXX_DEBUG but libB isn't, the std::vector<char> class in the main program and in libB actually have a different implementation, which leads to errors.

Upvotes: 1

Views: 521

Answers (2)

MSalters
MSalters

Reputation: 180020

It's probably most robust to create a void* createNDebugVec(char const* begin, char const* end); function, and put that in its own .cpp file. In that .cpp file, explicitly #undef GLIBCXX_DEBUG before including anything. This allows you to create a non-debug std::vector and return it pre-cast to void*. You'll probably need a matching void destroyNDebugVec(void*);.

Note that void* createNDebugVec(std::vector<char> const& src); won't work, for essentiall the same reason.

Upvotes: 1

PilouPili
PilouPili

Reputation: 2699

you can #undef _GLIBCXX_DEBUG before using the #include<vector> in your main program.

Just to be sure I would declare a non template class inheriting std::vector<char> in which you use the #undef _GLIBCXX_DEBUG trick and use the instance of this class to pass data to libB.

Upvotes: 1

Related Questions