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

Discussion in 'C++' started by nospam4314@gmail.com, Jan 18, 2007.

  1. 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?
    , Jan 18, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 18, 2007
    #2
    1. Advertising

  3. Ondra Holub Guest

    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.
    Ondra Holub, Jan 18, 2007
    #3
  4. ricardo Guest

    Thanks all for the replies.
    ricardo, Jan 18, 2007
    #4
  5. red floyd Guest

    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.
    red floyd, Jan 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel

    Including headers only once

    Miguel, Oct 30, 2003, in forum: C Programming
    Replies:
    3
    Views:
    303
  2. dont bother
    Replies:
    0
    Views:
    766
    dont bother
    Mar 3, 2004
  3. J Kenneth King
    Replies:
    8
    Views:
    379
    J Kenneth King
    Oct 24, 2008
  4. Nu
    Replies:
    11
    Views:
    151
    John Bokma
    Oct 31, 2006
  5. Replies:
    2
    Views:
    1,185
    Michele Dondi
    May 18, 2007
Loading...

Share This Page