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