473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

including c headers in c++ (NOT the one in FAQ)

I am trying to include a C header file in C++, but the C header has
some datastructures where some variable names are "namespace" and
"this". Obviously C++ complains about the use of these reserved words.
I was wondering if there is a way around it besides changing the source
code (mainly because I don't have access to the source code, so
changing the header files will only create more trouble).

I tried extern "C", but it didn't work.

Below is a small sample code I created that reproduces the problem.

foo.h:
#ifndef __FOO_H
#define __FOO_H

typedef struct
{
int namespace;
int this;
} foo_st;

void printfoo( foo_st* st );

void setfoo( foo_st* st, int n, int t );

#endif

foo.c:
#include <stdio.h>
#include "foo.h"

void setfoo( foo_st* st, int n, int t )
{
st->namespace = n;
st->this = t;
}
void printfoo( foo_st* st )
{
printf("foo->namespace = %d, foo->this = %d\n", st->namespace,
st->this );
}

bar.cpp:
#ifdef __cplusplus
extern "C"
{
#endif
#include "foo.h"
#ifdef __cplusplus
}
#endif
int main()
{
foo_st myfoo;
setfoo( & myfoo, 4, 5 );
printfoo( & myfoo );
return 0;
}

I am using gcc 3.3.2 and I compile with:
gcc foo.c -c
gcc bar.cpp foo.o

The error I get is:
In file included from bar.cpp:5:
foo.h:6: error: declaration does not declare anything
foo.h:6: error: syntax error before `namespace'
foo.h:7: error: declaration does not declare anything

(if I change bar.cpp to bar.c, it works as it should)

Can anyone help with that?

Jan 18 '07 #1
4 1501
no********@gmail.com wrote:
I am trying to include a C header file in C++, but the C header has
some datastructures where some variable names are "namespace" and
"this". Obviously C++ complains about the use of these reserved words.
I was wondering if there is a way around it besides changing the
source code (mainly because I don't have access to the source code, so
changing the header files will only create more trouble).

I tried extern "C", but it didn't work.

[..]
No, there is no way around it. You will need to copy the header
and fix the names. Another possible solution is to write a wrapper
around the functionality, in C, and give it another header, already
designed (and implemented) to be used in both C and C++ (C only if
it's important).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 18 '07 #2

no********@gmail.com napsal:
I am trying to include a C header file in C++, but the C header has
some datastructures where some variable names are "namespace" and
"this". Obviously C++ complains about the use of these reserved words.
I was wondering if there is a way around it besides changing the source
code (mainly because I don't have access to the source code, so
changing the header files will only create more trouble).

I tried extern "C", but it didn't work.

Below is a small sample code I created that reproduces the problem.

foo.h:
#ifndef __FOO_H
#define __FOO_H

typedef struct
{
int namespace;
int this;
} foo_st;

void printfoo( foo_st* st );

void setfoo( foo_st* st, int n, int t );

#endif

foo.c:
#include <stdio.h>
#include "foo.h"

void setfoo( foo_st* st, int n, int t )
{
st->namespace = n;
st->this = t;
}
void printfoo( foo_st* st )
{
printf("foo->namespace = %d, foo->this = %d\n", st->namespace,
st->this );
}

bar.cpp:
#ifdef __cplusplus
extern "C"
{
#endif
#include "foo.h"
#ifdef __cplusplus
}
#endif
int main()
{
foo_st myfoo;
setfoo( & myfoo, 4, 5 );
printfoo( & myfoo );
return 0;
}

I am using gcc 3.3.2 and I compile with:
gcc foo.c -c
gcc bar.cpp foo.o

The error I get is:
In file included from bar.cpp:5:
foo.h:6: error: declaration does not declare anything
foo.h:6: error: syntax error before `namespace'
foo.h:7: error: declaration does not declare anything

(if I change bar.cpp to bar.c, it works as it should)

Can anyone help with that?
Hi. You can change it with preprocessor in case you are compiling
everything:

#define this x_this
#define namespace x_namespace
#include "your_c_header.h"
#undef namespace
#undef this

However it is a dirty hack which breaks usual rules for including
headers. And it will not work, if your header represents API for some
library which you cannot recompile.

Jan 18 '07 #3
Thanks all for the replies.

Jan 18 '07 #4
no********@gmail.com wrote:
I am trying to include a C header file in C++, but the C header has
some datastructures where some variable names are "namespace" and
"this". Obviously C++ complains about the use of these reserved words.
I was wondering if there is a way around it besides changing the source
code (mainly because I don't have access to the source code, so
changing the header files will only create more trouble).

I tried extern "C", but it didn't work.

Below is a small sample code I created that reproduces the problem.

foo.h:
#ifndef __FOO_H
#define __FOO_H
Other people have provided possible solutions to your problem, so I'm
just going to be pedantic here.

Your include guard is really, REALLY bad.

Per the standard, any identifier with two consecutive underscores is
reserved to the implementation (i.e. the compiler and standard library
vendor is free to use it, but YOU may not).

Even if you removed one of the leading underscores (_FOO_H), it would
still be bad, because again, per the standard, any identifier with a
leading underscore followed by an upper case letter is similarly reserved.

I would recommend something like FOO_H_ for your include guard.
Jan 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the...
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
3
by: Donovan Martin | last post by:
Detecting dead headers is an extremely tiresome and lengthy process. Is there an automated utility available which might do this for me? That is, some utility that will check my .cpp and .h files...
21
by: Connell Gauld | last post by:
Hi, I have what feels like a really stupid question and I'm sorry if it is asked a lot. Imagine I have two classes cShip and cPassenger. They each have a definition in their own header cShip.h...
1
by: Ben | last post by:
Hi guys, this *should* be a simple problem, but I've never really got my head around it. I'm hoping somebody on this message board could help me out? I don't really have any experience with VS.NET,...
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
31
by: Joseph Wakeling | last post by:
Hello all, I'm writing some programs that will be using modules from the GNU Scientific Library (GSL). Include commands within the GSL modules have commands such as, #include...
5
by: =?Utf-8?B?SmltbWVy?= | last post by:
Hello, I've been trying to create a WCF SOAP Router Service that can forward not just the message body but also any security headers set by the originator of the message. The destination service...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.