View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# How to include C files in C++ program properly

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Misery  Members

Posted 14 June 2012 - 05:32 AM

Hello,

I am writing a library for numerical computations. The whole thing is written in C++. However, there are some interesting open source programs I would like to integrate with my library. For example Triangle mesh generator. For the problem lets say I have three files:

TypesAndDefines.h (Ansi C++ syntax)

Triangle.h (Ansi C syntax)
Triangle.c (Ansi C syntax) - souce file for Triangle.h

In general using c files from c++ program works fine, but what I tend to do is:

- I have abstract data types in C++ file TypesAndDefines.h setting for example:

		   #ifdef x64
typedef double REAL;
#elif
typedef float REAL;
#endif


- there are also namsespaces defined in this file

- I need in Triangle.h and Triangle.c those defined typedefs, but I cannot include it in C-syntax file.

The only working solution I have found is to make a C syntax compatibile version of TypesAndDefines.h file and include it.
However it would be better for the project to use only one file with definitions. Is there any good way to do this? I can of course cancel the C++ TypesAndDefines.h file but then I also cancel using general namespace for my library what is quite elegant and convinient for end user of the lib.

Misery

Edited by Misery, 14 June 2012 - 05:37 AM.

### #2Aardvajk  Members

Posted 15 June 2012 - 12:46 AM

#ifdef __cplusplus is probably what you want.

This link shows the kind of reverse use to what you want but principle is the same.

Anything C++-specific, wrap in #ifdef __cplusplus blocks. The C compiler won't define this before running the preprocessor so they will be skipped.

Simple example:

#ifdef __cplusplus
namespace foo
{
#endif
typedef double REAL;

#ifdef __cplusplus
}
#endif


You find this used a lot in third-party library headers.

Edited by Aardvajk, 15 June 2012 - 12:48 AM.

### #3Misery  Members

Posted 15 June 2012 - 06:37 AM

#ifdef __cplusplus is probably what you want.

This link shows the kind of reverse use to what you want but principle is the same.

Anything C++-specific, wrap in #ifdef __cplusplus blocks. The C compiler won't define this before running the preprocessor so they will be skipped.

Simple example:

#ifdef __cplusplus
namespace foo
{
#endif
typedef double REAL;

#ifdef __cplusplus
}
#endif


You find this used a lot in third-party library headers.

Yes, but what I want really, is to have somehow visible in C file variable type

foo::REAL

for example. Like

#define REAL foo::REAL


something like that.

Edited by Misery, 15 June 2012 - 06:38 AM.

### #4Aardvajk  Members

Posted 15 June 2012 - 04:28 PM

Not sure I fully understand, but :: is not a valid token in C and the preprocessor works on a token based system so I don't think there is any way you can do what you want, short of some kind of ungodly custom preprocessing.

Could you perhaps explain what you are trying to do? I don't really understand your requirement to compile Triangle.c as C rather than C++ if you have the source.

### #5Misery  Members

Posted 16 June 2012 - 02:20 AM

I'll try to explein better:
In a C syntax file I do have an abstract type REAL. In fact the mening of it is the same as my C++ REAL typedef. However it is qiute important for me to have only one definition of REAL's. And that is foo::REAL. It is done in C++ file. For example:

//C++ types adn defines file
namespace foo{
#ifdef x64
typedef double REAL;
#elif
typedef float REAL;
#endif
} // namespace foo

This is done in file that chooses built in C/C++ data types dependently on compiler and system.
What I am trying to do is to #define REAL in C file to be the same as foo::REAL. Something like that
(not that it would work, but I'll get the meaning):
#define REAL /*so that is the same as*/ foo::REAL //without making another definition of it in the manner:
#ifdef x64
#define REAL float;
#elif
#define REAL double;
#endif


So in fact the definition would be done only once.

### #6Brother Bob  Moderators

Posted 16 June 2012 - 04:44 AM

Make an implementation type defining the actual type of REAL, and define the REAL type as the implementation type in whatever scope you want.

#ifndef x64

typedef double _real_implementation_t;

#else

typedef float _real_implementation_t;

#end

#if __cplusplus

namespace foo {

typedef REAL _real_implementation_t;

}

#else

typedef REAL _real_implementation;

#endif



Naturally, you cannot have C++ syntax in a C compiler. So the scope resolution operator is not your only problem; the name space is as well.

Edited by Brother Bob, 16 June 2012 - 04:45 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.