CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2007
    Posts
    34

    Sharing struct between C and C++

    I am attempting to use C code from C++ and am having trouble sharing a struct between the two languages.

    Currently I am able to get everything to compile by defining the struct in both the C++ file and the C code. For example:

    MyFile.cpp
    Code:
    extern "C" {
    struct SomeStruct {
        short   One[904];
        short   two;
    };
    extern struct SomeStruct theStruct;
    }
    .
    .
    theStruct.two = 1;

    defines.h
    Code:
    struct SomeStruct {
        short   One[904];
        short   two;
    };
    extern struct SomeStruct theStruct;
    SomeCode.c
    Code:
    struct SomeStruct theStruct;
    .
    .
    theStruct.two = 0;
    There are other C files that use theStruct and they have
    Code:
    extern struct SomeStruct theStruct;
    in them.

    These snippets are from the real code with names changed but it shows what I currently have. I want to access the same variable of the struct type from both C++ and C.

    I have been struggling with this for a while now and can get it to compile doing it this way but I am unsure if it is the correct way to do this.

    Any help is greatly appreciated.

    Thanks.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Sharing struct between C and C++

    I would never recommend using extern to create global variables. I instead recommend something like a Meyers Singleton.

    defines.h
    Code:
    extern "C" {
        struct SomeStruct {
            short   One[904];
            short   two;
        };
    
        struct SomeStruct * getSomeStruct(void);
    }
    defines.cpp
    Code:
    #include "defines.h"
    struct SomeStruct * getSomeStruct(void){
        static struct SomeStruct mystruct;
        return &mystruct;
    }
    That way whenever you want to use the variable, call the method instead. extern is scary.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Sharing struct between C and C++

    Quote Originally Posted by Yadrif View Post
    Any help is greatly appreciated.
    C is a subset of C++ so couldn't you simply consider the C code to be C++?

    You integrate the C code as a low-level part of the C++ program and compile everything with the same C++ compiler.
    Last edited by nuzzle; April 18th, 2012 at 11:12 AM.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Sharing struct between C and C++

    Quote Originally Posted by nuzzle View Post
    C is a subset of C++ so couldn't you simply consider the C code to be C++?

    You integrate the C code as a low-level part of the C++ program and compile everything with the same C++ compiler.
    Not entirely... the following is valid C code, but not valid C++ code.
    Code:
    char * hello = "hello world";
    Code:
    int i = 20;
    char dynamically_sized_array_on_stack[i];
    I assume most C++ compilers will handle both as expected, g++ just gives you a warning for the implicit const dropping and doesn't even mention the variable as an array size. So if the OP is using gcc, its fine, but I'd be willing to bet the VC++ compiler will handle neither.



    Yadrif: How are you compiling? Are you giving all the files to the C++ compiler, or are you mixing them? Also, why are you mixing the two? Couldn't you just write what you are doing in C?
    Last edited by ninja9578; April 18th, 2012 at 01:58 PM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sharing struct between C and C++

    Quote Originally Posted by ninja9578 View Post
    I assume most C++ compilers will handle both as expected, g++ just gives you a warning for the implicit const dropping and doesn't even mention the variable as an array size.
    Microsoft Visual Studio won't compile the second example, because it doesn't support C99. Technically, even a compiler supporting C99 shouldn't handle it if it occurs in a C++ file, but many do because they have the logic written anyway.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Sharing struct between C and C++

    Quote Originally Posted by ninja9578 View Post
    Not entirely...
    No not entirely but it's an approach worth trying. Chances are plain C code is also C++ or have just minor deviations. In practice a port should be fairly undramatic, especially if there are just a few files.
    Last edited by nuzzle; April 18th, 2012 at 11:46 PM.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Sharing struct between C and C++

    Quote Originally Posted by ninja9578 View Post
    I would never recommend using extern to create global variables. I instead recommend something like a Meyers Singleton.
    Communication via shared global data is always a bad idea. Using a Singleton doesn't change that. If possible it should be avoided regardless of whether the OP keeps the C and C++ codes separate or merges everything into one unified C++ code.
    Last edited by nuzzle; April 19th, 2012 at 12:13 AM.

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Sharing struct between C and C++

    Quote Originally Posted by Lindley View Post
    Technically, even a compiler supporting C99 shouldn't handle it if it occurs in a C++ file, but many do because they have the logic written anyway.
    Are you sure? Does the standard say somewhere that it can't? Or is it simply undefined and left up to the compiler. I'm sure g++ only compiles it because it shares most of its codebase with gcc.


    And yes, nuzzle, it might work. But my assumption is that he s extending C functionality written by someone else. A professional C developer might use features defined in C99, but not in C++11/c++03. If it works great, most of the time it will, I just wanted to point out that there are a few incompatibilities that the OP might come across trying to compile C code as C++.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sharing struct between C and C++

    Quote Originally Posted by ninja9578 View Post
    Are you sure? Does the standard say somewhere that it can't? Or is it simply undefined and left up to the compiler. I'm sure g++ only compiles it because it shares most of its codebase with gcc.
    The standard doesn't forbid it.

    And yes, nuzzle, it might work. But my assumption is that he s extending C functionality written by someone else. A professional C developer might use features defined in C99, but not in C++11/c++03. If it works great, most of the time it will, I just wanted to point out that there are a few incompatibilities that the OP might come across trying to compile C code as C++.
    It's actually much more common than you might think to find C code that won't build as C++. Functions with no return type specified and malloc calls without a cast are just two of the many things that work in C but not C++.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured