473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

including c headers in c++

3 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. #ifndef __FOO_H
  2. #define __FOO_H
  3.  
  4. typedef struct 
  5. {
  6.    int namespace;
  7.    int this;
  8. } foo_st;
  9.  
  10. void printfoo( foo_st* st );
  11.  
  12. void setfoo( foo_st* st, int n, int t );
  13.  
  14. #endif
  15.  
foo.c:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "foo.h"
  3.  
  4. void setfoo( foo_st* st, int n, int t )
  5. {
  6.    st->namespace = n;
  7.    st->this = t;
  8. }
  9.  
  10.  
  11. void printfoo( foo_st* st )
  12. {
  13.    printf("foo->namespace = %d, foo->this = %d\n", st->namespace, st->this );
  14. }
  15.  
bar.cpp:
Expand|Select|Wrap|Line Numbers
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include "foo.h"
  6. #ifdef __cplusplus
  7. }
  8. #endif
  9. int main()
  10. {
  11.    foo_st myfoo;
  12.    setfoo( & myfoo, 4, 5 );
  13.    printfoo( & myfoo );
  14.    return 0;
  15. }
  16.  
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
1 1594
Motoma
3,237 Recognized Expert Specialist
I also get an error, however mine is a little different than yours.
Mine reads:

error: expected unspecified error before "namespace"
error: expected unspecified error before "this"
After having compiled the C code, you no longer need to maintain the invalid names. You can easily change the foo.h struct to :

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.   int ns;
  4.   int th;
  5. } foo_st;
  6.  
And code your C++ files using the new names. I know it's not the best solution, but it reduces the renaming from changing all of your source files to only changing the C++ files.
Jan 18 '07 #2

Sign in to post your reply or Sign up for a free account.

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...
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...
1
by: Martin Mücke | last post by:
I got a website consisting of about 150 php pages. The site uses a frameless table based design. Header and menu are always the same and therefore should be extracted. At the moment I got a...
3
by: Maciek | last post by:
All, I'm having a problem building a library (VMime) with Visual .NET 2003. It's including, among others, gnutls.h from the GNU TLS library and I'm getting syntax errors in that file. For...
4
by: nospam4314 | last post by:
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...
8
by: squaretriangle | last post by:
Regarding either of the following: 1. Including a .c file with #include, 2. Including source code contained in a .h header file with #include I have 2 questions. 1. Is the reality of most...
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
8
by: J Kenneth King | last post by:
Hey everyone, I'm working on a python extension wrapper around Rob Hess' implementation of a SIFT feature detector. I'm working on a computer-vision based project that requires interfacing with...
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
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.