473,804 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1519
no********@gmai l.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********@gmai l.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********@gmai l.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
1944
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 dependency explicit, or do just allow the include to be make implicitly through the .h include? That is, should the header of your .cpp file be: #include "CustomClass.h"
12
2003
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
1608
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 and determine which headers that are referenced are unnecessary?
21
1424
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 and cPassenger.h and their implementation in cShip.cpp and cPassenger.cpp. Now here is the header file for each: #ifndef CSHIP_H #define CSHIP_H
1
1098
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, which is probably why I'm having so much trouble! Basically, I've been doing some OpenGL coding using VS.NET as the compiler. Obviously (if you know any OGL), you'll know I need to include gl.h and glu.h to have access to all the drawing...
8
3053
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 structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
31
3610
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 <gsl/gsl_rng.h> (for example). Yet these header files aren't actually in the gsl
5
4136
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 I'm routing messages to uses WSHttpBinding, SSL with UserName/Password client credentials. Using guidance from the Technology samples I can create a router that forwards messages without security credentils but not with them. Can anybody point me...
8
4596
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.