How to include C files in C++ program properly

Recommended Posts

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:

- 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.

Share this post

Link to post

Share on other sites

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.

0

Share this post

Link to post

Share on other sites

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:

[code]
//C++ types adn defines file
namespace foo{
#ifdef x64
typedef double REAL;
#elif
typedef float REAL;
#endif
} // namespace foo
[/code]
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):
[code]
#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
[/code]