|
-
April 18th, 2012, 08:30 AM
#1
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.
-
April 18th, 2012, 09:35 AM
#2
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.
-
April 18th, 2012, 11:06 AM
#3
Re: Sharing struct between C and C++
 Originally Posted by Yadrif
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.
-
April 18th, 2012, 01:54 PM
#4
Re: Sharing struct between C and C++
 Originally Posted by nuzzle
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.
-
April 18th, 2012, 01:58 PM
#5
Re: Sharing struct between C and C++
 Originally Posted by ninja9578
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.
-
April 18th, 2012, 11:21 PM
#6
Re: Sharing struct between C and C++
 Originally Posted by ninja9578
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.
-
April 18th, 2012, 11:51 PM
#7
Re: Sharing struct between C and C++
 Originally Posted by ninja9578
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.
-
April 18th, 2012, 11:53 PM
#8
Re: Sharing struct between C and C++
 Originally Posted by Lindley
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++.
-
April 19th, 2012, 09:27 AM
#9
Re: Sharing struct between C and C++
 Originally Posted by ninja9578
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|