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


G

Guest

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?
 
Ad

Advertisements

V

Victor Bazarov

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
 
O

Ondra Holub

(e-mail address removed) 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.
 
Ad

Advertisements

R

red floyd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top