Access a c struct from C++ code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joet
    New Member
    • Jul 2008
    • 3

    Access a c struct from C++ code

    I have two files- call them ccode.h and c++.h. The file ccode.h has a struct in it that I would like to access from the c++ code. I have included ccode.h in c++.h but am not sure as to why this is not compiling. Here is an example:

    C Code:
    typedef struct
    {
    int y;
    int z;
    } game;

    C++ Code:
    #include "ccode.h"

    game test;

    Any help would be appreciated.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I have included ccode.h in c++.h but am not sure as to why this is not compiling.
    Give us a compileable snippet of code. That is, I can take it, compile it, and have it demonstrate the only problem you are having.

    Moreover, how do you know the code is not compiling. Do you, perhaps, get a compiler error? Or a linker error? You need to post them, verbatim (as in, you hit copy, then paste) so we can examine the errors for ourselves.

    Also, when posting code, use CODE tags. The usage of CODE tags is mentioned in the forum rules and FAQs. Look at the posting guidelines in the forum.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You need to add

      Code:
      /* ccode.h */
      #ifdef  __cplusplus
      extern "C" {
      #endif
      
      /* Your structure definition and other c declarations here */
      
      #ifdef  __cplusplus
      }
      #endif
      into your c header. When included into a C++ file the compiler needs to know what is actually c code so it can compile it correctly. The extern "C" block does this. However a C compiler would not understand that so you use the _cplusplus symbol to make it only visible to the C++ compiler.

      Open any standard C header for you compiler and you will see it in use.

      Comment

      • joet
        New Member
        • Jul 2008
        • 3

        #4
        Okay to the first poster- sorry I missed your code tags- I am a new poster. I have tried the ifdef cplusplus and the Error I get is Line 2:game is not defined.
        When I remove the game test line it compiles.
        Code:
        typedef struct
        {
        int y;
        int z;
        } game;
        
        C++ Code:
        #include "ccode.h"
        
        game test;

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Here's an example of a compileable snippet:

          Code:
          extern "C"
          {
              typedef struct
              {
                  int y;
                  int z;
              } game;
          }
          
          game test;
          
          int main() {}
          It actually compiles. I presume your test block of code is very similar to this?

          Comment

          • joet
            New Member
            • Jul 2008
            • 3

            #6
            The code you entered is the same- however- I have solved the problem. Evidently there are two things going on. Since the code itself was in a header file- it compiled without the extern C directive. I commented it out a one point so that I could see what else was happening. In the actual code I was using 2 different header files. There was a variable name above my declaration
            Code:
             game test;
            that was conflicting with a variable in the other header file. Thank you for the help- and I hope that my explanation will help someone else that encounters a similar issue.

            Comment

            Working...