473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Access a c struct from C++ code

3
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.
Jul 22 '08 #1
5 1475
oler1s
671 Expert 512MB
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.
Jul 22 '08 #2
Banfa
9,065 Expert Mod 8TB
You need to add

Expand|Select|Wrap|Line Numbers
  1. /* ccode.h */
  2. #ifdef  __cplusplus
  3. extern "C" {
  4. #endif
  5.  
  6. /* Your structure definition and other c declarations here */
  7.  
  8. #ifdef  __cplusplus
  9. }
  10. #endif
  11.  
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.
Jul 22 '08 #3
joet
3
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.
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. int y;
  4. int z;
  5. } game;
  6.  
  7. C++ Code:
  8. #include "ccode.h"
  9.  
  10. game test;
  11.  
Jul 23 '08 #4
oler1s
671 Expert 512MB
Here's an example of a compileable snippet:

Expand|Select|Wrap|Line Numbers
  1. extern "C"
  2. {
  3.     typedef struct
  4.     {
  5.         int y;
  6.         int z;
  7.     } game;
  8. }
  9.  
  10. game test;
  11.  
  12. int main() {}
It actually compiles. I presume your test block of code is very similar to this?
Jul 23 '08 #5
joet
3
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
Expand|Select|Wrap|Line Numbers
  1.  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.
Jul 23 '08 #6

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

Similar topics

2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
7
by: CaptainBly | last post by:
Okay I've been spending lots of time on this and it's giving me a migraine so I go humble and ask the guru's here. I have several structs typedef struct vert { int x,y,z; struct vert *...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...

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.