Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C code in C++ and used inside class

Reply
Thread Tools

C code in C++ and used inside class

 
 
cognacc@
Guest
Posts: n/a
 
      08-04-2009
Im trying to use some C code inside a C++ class, this class is used
in another C++ class ofcourse, or will be.
The C++ class will contain a data structure
from the C code amongst others.

But in the C code header file where the datatype is defined, there are
also some global variables defined.
Is there a way to avoid multiple inclusion error, without changing the
C code?

Michael
 
Reply With Quote
 
 
 
 
Francesco
Guest
Posts: n/a
 
      08-04-2009
On 4 Ago, 19:21, "cognacc@" <michael.cogn...@gmail.com> wrote:
> Im trying to use some C code inside a C++ class, this class is used
> in another C++ class ofcourse, or will be.
> The C++ class will contain a data structure
> from the C code amongst others.
>
> But in the C code header file where the datatype is defined, there are
> also some global variables defined.
> Is there a way to avoid multiple inclusion error, without changing the
> C code?
>
> Michael


Hi Michael,
just the two cents of an hobbyist <-- assume this disclaimer of mine,
first of all.

If you get multiple inclusion error, I suppose that the globals
defined in the C header are outside of the #ifndef / #define / #endif
block of macros (assuming such macros are there).

If your statement "without changing the C code" means that you don't
want to touch the C header *at all*, I suppose there is no way to
avoid such a multiple-inclusion risk (at least, not as long as other
people is involved in coding it) otherwise you could simply add a
further block of conditional macros wrapping the header's content -
quite easy to automate too, in case you've got many of such
"problematic" headers to include.

I think that giving more details will raise more interest in your
issue. You could post some code to illustrate it better (the C header
in particular). Then maybe some of the more experienced could jump in.

Have good time coding,
cheers,
Francesco
 
Reply With Quote
 
 
 
 
cognacc
Guest
Posts: n/a
 
      08-05-2009
On Aug 4, 9:49*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> cognacc@ wrote:
> > Im trying to use *some C code inside a C++ class, this class is used
> > in another C++ class ofcourse, or will be.
> > The C++ class will contain a *data structure
> > from the C code amongst others.

>
> > But in the C code header file where the datatype is defined, there are
> > also some global variables defined.

>
> That's A VERY BAD IDEA(tm). *Variables should never be *defined* in headers.


I mean declared sorry. if in header:
int Ggloabalvar; // this is a declaration right


s_57_defs.h this is the file with a few globals vars
and also a datatype conversion, works fine compiled as C.
-----------------------------------------------------------------------------
#ifndef _S_57_DEFS_H
#define _S_57_DEFS_H

/*
* File: s_57_defs.h
* Author: run
*
* Created on March 10, 2009, 3:07 PM
*/


#ifdef __cplusplus
extern "C" {
#endif

#include <sys/queue.h>

typedef char octet;

typedef char tag[5]; //! \todo ? check this typedef, see page 146 k&r

// Leader is 24 octets (bytes long) both DDR an DR leader (S-57)
#define LEADER_SIZE 24

// Fieldtag size ie VRID, is 4 octets long, specified int S-57
#define FIELD_TAG_SIZE 4

// SHOULD Probably read this and check, but not now
// field RP 09
#define DDR_FIELD_CONTROL_LENGTH 9

// See iso8211 table 1 delimiter and their uses
// Unit terminator : printable '&' : Terminates a unit in a field
#define UT 0x1F // dec: 31 oct: \037

// Field terminator : printable ';' : Terminates a complete field(ex:
VRID)
#define FT 0x1E // dec: 30 oct: \036
// This could be defined for each field (vrid, frid etc.)


#define STD_SIZE_LEN 220

struct GNAME {int len; char val[10];};

//
================================================== =============================
// global variables for conversion between octets to datatypes

// bXY : X is signednes Y is width in octets
unsigned char S57TYPE_b11; // UNSIGNED ONE OCTET CHAR
signed char S57TYPE_b21; // SIGNED ONE OCTET UNSIGNED CHAR
unsigned short S57TYPE_b12; // UNSIGNED TWO OCTET UNSIGNED CHAR
signed short S57TYPE_b22; // SIGNED TWO OCTET SIGNED CHAR
unsigned long S57TYPE_b14; //
signed long S57TYPE_b24; //

unsigned char S57TYPE_B5[5];
unsigned char S57TYPE_B8[8];
unsigned char S57TYPE_B[20]; // any large enough number
// DUMMY just to shut compiler up

char S57TYPE_A[200];
char S57TYPEA_UL[200]; // ascii unspecified length
char S57TYPEA_L8[8]; // which lengths

char S57TYPE_I[200]; // type i of generic legnth, real
length seen at data read time

double S57TYPE_R; // \note correct S57TYPE long ?
- think its incorrect!?

int LENGTH_TYPESIZE; // length to send as
parameter to function

// User defined define a head of a new queue
// Should define a tailqueue , a new call to getRecordSet() will reuse
same queue

TAILQ_HEAD(tailhead, DR_Data) dr_head;

typedef struct DR_Data // DR_Data is tag
{
char fieldtagname[5]; // the tagname, like VRID FOID DSSI, can be
used to look at the S57TYPE (if NAME = cast S57TYPE)
//BOOL is_toplevel; // is a toplevel field (ex: VRID)
int is_toplevel;
// record numbers used for debugging data structures
int dr_rec_nr; // total records ie FIELD numbers

int dr_toplevel_nr; // top level FIELDS (VRID, FOID, DSID, DSSI, ..)
number of toplevel fields, called a "RECORD BLOCK"
int dr_repeat_nr; // repeating fields number, same fields repeating
count
// int dr_different_fields // total fields exclusive repeating
fields (counted once)
//-----------------
void *field_ptr; // void pointer to a field a HUGE case for
casting to field S57TYPE (or look out for aut5o generating)
struct DR_Data *next_data; // DR_Data is tag
// LIST struct list_head somename see man queue (LIST*)
TAILQ_ENTRY(DR_Data) dr_data_list;
}DR_Data; // DR_Data i typedef

//typedef _dr_data DR_Data;

DR_Data *dr_data_ptr, *dr_data_pnext, *dr_data_pprev;

struct tailhead *pos_data, *q_data, *dr_head_ptr;

void usage(char **arg);

// DR_Data *
int readRecordSet(char * s57_filename);

void queue_init(void);

#ifdef __cplusplus
}
#endif


#endif /* _S_57_DEFS_H */
-----------------------------------------------------------------------

> > Is there a way to avoid multiple inclusion error, without changing the
> > C code?

> You could try creating another header with double inclusion guards and
> include that C header there, but it looks like you'd be better off
> rewriting the C stuff to make it proper.


Double inclusion guards? how?

Here is a C++ file using the data structure:
DR_Data *genericDRData; is the type from C i need to use in my class
---
#ifndef CHARTINFO_H
#define CHARTINFO_H

#include <string>

extern "C"
{
#include <stdlib.h>
#include <assert.h>

#include "../../decode_seacharts/s_57_defs.h" // <---- header with
datatype and global var, we only use the type(s)
}

// inner struct / class
// latitude and longtiude coordinates
struct LatLon
{
signed long lat; // Latitude
signed long lon; // Longtitude
};

struct XYPos
{
int lat; // Latitude
int lon; // Longtitude
char * unit; // m or km // define domains ?
};

class ChartInfo
{
public:
ChartInfo();

void loadRecordSet(char *filename);
long long getLNAME_id(); // RCNM '+' RCID
long long getNAME_id();
signed long getLattitude();
signed long getLongtitude();
int translateLatLon_to_meters(struct LatLon ll);
int translateLatLon_to_kilometers(struct LatLon ll);

// test function traverse all dr_data
void traverseAllDRData();

private: // private functions

long long getRecordSet_key();
double convertLatLon_to_degree(signed long lon_or_lat);

//=======================================
// private variables and data
DR_Data *genericDRData; // <----- a pointer to use with tailq
maybe readRecordSet should return that
//..
// RCNM b11 + RCID b14 => name_key_identifier
long long name_record_identifier;

// lname AGEN + FIDN + FIDS -> lname_key_identifier
long long lname_record_identifier;

};

#endif // CHARTINFO_H
---

Please tell if i post to much code (or to little, i have cleaned it up
some,
for easier reading and removed a few lines i was trying out).

I can Change the C file but i want to avoid that, in C declaring
common variables
in a header is pretty normal i think.

So maybe what im trying to do is write a small wrapper, but i never
done that before.


Mic

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-05-2009
On Aug 4, 9:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> cognacc@ wrote:
> > Im trying to use some C code inside a C++ class, this class
> > is used in another C++ class ofcourse, or will be. The C++
> > class will contain a data structure from the C code amongst
> > others.


> > But in the C code header file where the datatype is defined,
> > there are also some global variables defined.


> That's A VERY BAD IDEA(tm). Variables should never be
> *defined* in headers.


Historically, the distinction between definition and declaration
without definition is less clear in C. I think the current C
standard says basically the same thing as the C++ standard, in
much more round-about way---for once, the C++ standard is
considerably clearer. But historically, a lot of C compilers
treated file scope declarations much like a named common in
Fortran (sort of like C++ treats the definition of a static data
member of the instantiation of a class template). Technically,
code which depends on this violates the C equivalent of the one
definition rule; practically, such code exists, and (some? many?
most?) compilers cater to it. Thus, if I have two source files,
a.c and b.c, each with "int a;" at global (file) scope, g++ will
complain if I compile and link them as C++, but gcc accepts the
code.

If I understand the orginal poster's problem, he has just such a
C header file, and is trying to include it in a C++ program and
compile it as C++.

> > Is there a way to avoid multiple inclusion error, without
> > changing the C code?


> You could try creating another header with double inclusion
> guards and include that C header there, but it looks like
> you'd be better off rewriting the C stuff to make it proper.


He'll probably end up having to rewrite a header for use in C++,
yes. If he has access to the C code, the best solution would be
to design the new header for dual use, and use it exclusively.
Especially as the current header counts on something that is
formally undefined behavior in C as well.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-05-2009
* James Kanze:
> On Aug 4, 9:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> cognacc@ wrote:
>>> Im trying to use some C code inside a C++ class, this class
>>> is used in another C++ class ofcourse, or will be. The C++
>>> class will contain a data structure from the C code amongst
>>> others.

>
>>> But in the C code header file where the datatype is defined,
>>> there are also some global variables defined.

>
>> That's A VERY BAD IDEA(tm). Variables should never be
>> *defined* in headers.

>
> Historically, the distinction between definition and declaration
> without definition is less clear in C. I think the current C
> standard says basically the same thing as the C++ standard, in
> much more round-about way---for once, the C++ standard is
> considerably clearer. But historically, a lot of C compilers
> treated file scope declarations much like a named common in
> Fortran (sort of like C++ treats the definition of a static data
> member of the instantiation of a class template). Technically,
> code which depends on this violates the C equivalent of the one
> definition rule; practically, such code exists, and (some? many?
> most?) compilers cater to it. Thus, if I have two source files,
> a.c and b.c, each with "int a;" at global (file) scope, g++ will
> complain if I compile and link them as C++, but gcc accepts the
> code.
>
> If I understand the orginal poster's problem, he has just such a
> C header file, and is trying to include it in a C++ program and
> compile it as C++.
>
>>> Is there a way to avoid multiple inclusion error, without
>>> changing the C code?

>
>> You could try creating another header with double inclusion
>> guards and include that C header there, but it looks like
>> you'd be better off rewriting the C stuff to make it proper.

>
> He'll probably end up having to rewrite a header for use in C++,
> yes. If he has access to the C code, the best solution would be
> to design the new header for dual use, and use it exclusively.
> Especially as the current header counts on something that is
> formally undefined behavior in C as well.


Not sure about that.

As I recall, at least C99 has "tentative" filescope definitions.


Cheers,

- Alf
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 12:12 pm, Hendrik Schober <spamt...@gmx.de> wrote:
> cognacc wrote:
> > On Aug 4, 9:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> >> cognacc@ wrote:
> >>> Im trying to use some C code inside a C++ class, this
> >>> class is used in another C++ class ofcourse, or will be.
> >>> The C++ class will contain a data structure from the C
> >>> code amongst others. But in the C code header file where
> >>> the datatype is defined, there are also some global
> >>> variables defined.
> >> That's A VERY BAD IDEA(tm). Variables should never be
> >> *defined* in headers.


> > I mean declared sorry. if in header:
> > int Ggloabalvar; // this is a declaration right

>
> No, it's a definition.


Yes, but multiple definitions are undefined behavior. A
compiler is free to accept them. Traditionally, C compilers do,
and C++ compilers don't; a quick experiment here showed that
when compiling C, gcc, Sun cc/CC and VC++ all accept the
multiple definitions without a complaint; if I compile as C++,
only Sun CC accepts them.

> The declaration would like this:
> extern int Ggloabalvar;


And
extern int Ggloabalvar = 42 ;
would again be a definition. The rules as to what is and is not
a definition are not very orthogonal, having to deal with many
historical issues, as they do.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
cognacc
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 1:42*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Aug 5, 12:12 pm, Hendrik Schober <spamt...@gmx.de> wrote:
>
> > cognacc wrote:
> > > On Aug 4, 9:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> > >> cognacc@ wrote:
> > >>> Im trying to use *some C code inside a C++ class, this
> > >>> class is used in another C++ class ofcourse, or will be.
> > >>> The C++ class will contain a *data structure from the C
> > >>> code amongst others. *But in the C code header file where
> > >>> the datatype is defined, there are also some global
> > >>> variables defined.
> > >> That's A VERY BAD IDEA(tm). *Variables should never be
> > >> *defined* in headers.
> > > I mean declared sorry. if in header:
> > > int Ggloabalvar; *// this is a declaration right

>
> > No, it's a definition.

>
> Yes, but multiple definitions are undefined behavior. *A
> compiler is free to accept them. *Traditionally, C compilers do,
> and C++ compilers don't; a quick experiment here showed that
> when compiling C, gcc, Sun cc/CC and VC++ all accept the
> multiple definitions without a complaint; if I compile as C++,
> only Sun CC accepts them.
>
> > The declaration would like this:
> > * *extern int Ggloabalvar;

>
> And
> * * extern int Ggloabalvar = 42 ;



but in C if you define in global scope extern is implicit, as i
understand K&R.
therefore my
int globavar; in global header.
is the same as extern int globavar; if i understandd this correctly.
?

mic
 
Reply With Quote
 
cognacc
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 12:12 pm, Hendrik Schober <spamt...@gmx.de> wrote:
> cognacc wrote:
> > On Aug 4, 9:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> >> cognacc@ wrote:
> >>> Im trying to use some C code inside a C++ class, this class is used
> >>> in another C++ class ofcourse, or will be.
> >>> The C++ class will contain a data structure
> >>> from the C code amongst others.
> >>> But in the C code header file where the datatype is defined, there are
> >>> also some global variables defined.
> >> That's A VERY BAD IDEA(tm). Variables should never be *defined* in headers.

>
> > I mean declared sorry. if in header:
> > int Ggloabalvar; // this is a declaration right

>
> No, it's a definition.
> The declaration would like this:
> extern int Ggloabalvar;
>


Ok then the problem is that i use these globals in multiple files i
would then have to add
the definiton of the variables in each file that uses these globals,
(somewhere it said i can only
define a variable in one place.)
These globals are used as transfer variables. between functions,
because the design wold be clunky
if passing as a parameter function (because of some autogenerated
files in the decoder. its also a prototype.

It works flawlessky in C (where this method of sharing globals seems
common)
It is only when there is multiple translation units in C++ that the
multiple definition error occurs.
not with multiple translation units combined by C compiler

so the decoder use some C ism not compatible with C++.

I still want to learn if there is a way to use it without changing the
C
files if i i come upon some code i can't change.

otherwise i plan to split the code into, types , transfervars, and
defines.



(is the extern keyword different wether its in global scope or in
function?)
 
Reply With Quote
 
cognacc
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 2:33*pm, cognacc <michael.cogn...@gmail.com> wrote:
> On Aug 5, 1:42*pm, James Kanze <james.ka...@gmail.com> wrote:


> > Yes, but multiple definitions are undefined behavior. *A
> > compiler is free to accept them. *Traditionally, C compilers do,
> > and C++ compilers don't; a quick experiment here showed that
> > when compiling C, gcc, Sun cc/CC and VC++ all accept the
> > multiple definitions without a complaint; if I compile as C++,
> > only Sun CC accepts them.

>


> but in C if you define in global scope extern is implicit, as i
> understand K&R.
> therefore my
> int globavar; in global header.
> is the same as extern int globavar; if i understandd this correctly.
> ?

certainly my compiler seems to disagree -
Does it depends on how i include it?

mic

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-05-2009
cognacc wrote:
> On Aug 5, 2:33 pm, cognacc <michael.cogn...@gmail.com> wrote:
>> On Aug 5, 1:42 pm, James Kanze <james.ka...@gmail.com> wrote:

>
>>> Yes, but multiple definitions are undefined behavior. A
>>> compiler is free to accept them. Traditionally, C compilers do,
>>> and C++ compilers don't; a quick experiment here showed that
>>> when compiling C, gcc, Sun cc/CC and VC++ all accept the
>>> multiple definitions without a complaint; if I compile as C++,
>>> only Sun CC accepts them.

>
>> but in C if you define in global scope extern is implicit, as i
>> understand K&R.
>> therefore my
>> int globavar; in global header.
>> is the same as extern int globavar; if i understandd this correctly.
>> ?

> certainly my compiler seems to disagree -
> Does it depends on how i include it?


What exactly do you mean by "how"? Double-quotes versus angle brackets?
I don't think it should matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry ASP .Net 0 03-01-2004 11:48 PM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry ASP .Net 0 03-01-2004 11:48 PM



Advertisments