I am doing cross compiling between C++ and C compiler for header files and desperately need help on figuring out namespace issue error generated by C++ compiler.

Here are two header files:abc1.h, abc2.h and a C/C++ file containing the main().

When I compile this code on C, it works fine. However whenever I compile this on C++ compiler. It gives me the following error:
g++ -g -Wall cpptest.c -o cpptest
"abc2.h:3: error: using typedef-name ‘abc’ after ‘struct’
abc1.h:9: error: ‘abc’ has a previous declaration here"

I need to have both data structure available (struct _abc and struct abc)
someone typedef the _abc to abc inside abc1.h and I cannot change this file. I can only change abc2.h and the C/C++ file.
Anyone can explain to me why C++ compiler cannot accept this?
Anyone know of solution without changing abc1.h? (a work around)

/*********** Header file abc1.h************/
#ifndef __ABC1__
struct _abc
{
    int a;
    int b;
}/*abc*/;
/* if I comment out The line below and uncommented the abc above, it works well. 
    typedef seems to be an issue in C++ compiler. However I can not touch this header file. */
typedef struct _abc    abc;
#endif
/*********** Header file abc2.h************/
#ifndef __ABC2__
struct abc
{
    char *c;
    void *d;
};
#endif
/* C/C++ file named abc.c or abc.cpp */
#include "abc1.h"
#include "abc2.h"
#include <stdio.h>
int main()
{
    printf("testing \n");
    return 0;
}

Recommended Answers

All 2 Replies

You're defining abc in two places, and they're getting put into the same source file. I don't know what you expected to happen.

I want to know if there is a work around and the reason why C++ compiler generates error while C compiler is fine with this.

I removed the comment from the abc1.h header file since it is not right.

/*********** Header file abc1.h************/
#ifndef __ABC1__
struct _abc
{
int a;
int b;
};
typedef struct _abc abc;
#endif

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.