Header files, C++ vs C compiler question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 2
Reputation: toonces is an unknown quantity at this point 
Solved Threads: 0
toonces toonces is offline Offline
Newbie Poster

Header files, C++ vs C compiler question

 
0
  #1
Sep 7th, 2007
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)

  1. /*********** Header file abc1.h************/
  2. #ifndef __ABC1__
  3. struct _abc
  4. {
  5. int a;
  6. int b;
  7. }/*abc*/;
  8. /* if I comment out The line below and uncommented the abc above, it works well.
  9.   typedef seems to be an issue in C++ compiler. However I can not touch this header file. */
  10. typedef struct _abc abc;
  11. #endif

  1. /*********** Header file abc2.h************/
  2. #ifndef __ABC2__
  3. struct abc
  4. {
  5. char *c;
  6. void *d;
  7. };
  8. #endif

  1. /* C/C++ file named abc.c or abc.cpp */
  2. #include "abc1.h"
  3. #include "abc2.h"
  4. #include <stdio.h>
  5. int main()
  6. {
  7. printf("testing \n");
  8. return 0;
  9. }
Last edited by toonces; Sep 7th, 2007 at 10:55 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Header files, C++ vs C compiler question

 
1
  #2
Sep 7th, 2007
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.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: toonces is an unknown quantity at this point 
Solved Threads: 0
toonces toonces is offline Offline
Newbie Poster

Re: Header files, C++ vs C compiler question

 
0
  #3
Sep 7th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC