Is there any way to skip macro replacement(During preprocessing) when macro is used as a variable name?

644 views Asked by At

ConflictHeader.h

      #define _c 6 //This is third party header, canot change, since
// there is no sorce code to rebuild

testclass.h

    #ifndef TESTCLASS_H
    #define TESTCLASS_H

    #include <QObject>
    #include "ConflictHeader.h"//Include conflicted header

    class TestClass : public QObject
    {
      Q_OBJECT

    public:
        TestClass(QObject *parent);
        ~TestClass();
    private:

    };
    #endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"

TestClass::TestClass(QObject *parent)
  : QObject(parent)
{

}    
TestClass::~TestClass()
{

}

moc_testclass.cpp

This is generated by MOC compiler, Please note function where compilation issue comes "int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)"

/****************************************************************************
** Meta object code from reading C++ file 'testclass.h'
**
** Created: Wed Jun 10 11:24:06 2015
**      by: The Qt Meta Object Compiler version 62 (Qt 4.7.4)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include "../../testclass.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'testclass.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.7.4. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_TestClass[] = {

 // content:
       5,       // revision
       0,       // classname
       0,    0, // classinfo
       0,    0, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

       0        // eod
};

static const char qt_meta_stringdata_TestClass[] = {
    "TestClass\0"
};

const QMetaObject TestClass::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_TestClass,
      qt_meta_data_TestClass, 0 }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &TestClass::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *TestClass::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *TestClass::qt_metacast(const char *_clname)
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_TestClass))
        return static_cast<void*>(const_cast< TestClass*>(this));
    return QObject::qt_metacast(_clname);
}

int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    return _id;
}
QT_END_MOC_NAMESPACE

Compiler output error 1>.\GeneratedFiles\Debug\moc_testclass.cpp(62) : error C2143: syntax error : missing ')' before 'constant'

So I cannot change "_c" in either "ConflictHeader.h"( thirdparty lib) and "moc_testclass.cpp"( moc generated ).

Is there any way to skip macro replacement(During preprocessing) when macro is used as a variable name?

2

There are 2 answers

0
R Sahu On BEST ANSWER

You should be able to fix that problem by using:

#include "ConflictHeader.h"//Include conflicted header
#ifdef _c
#undef _c
#endif

A better option might be to create a wrapper .h file fro ConflictHeader.h and use the wrappr .h file in rest of your code.

ConflictHeaderW.h:

#pragma once

#include "ConflictHeader.h"
#ifdef _c
#undef _c
#endif

// #undef anything else that create problems.
0
skrtbhtngr On

Use #undef preprocessor directive to undefine the macro.

More info: https://gcc.gnu.org/onlinedocs/cpp/Undefining-and-Redefining-Macros.html