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

This topic is 2227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
#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:

[source]
#ifdef __cplusplus
namespace foo
{
#endif
typedef double REAL;

#ifdef __cplusplus
}
#endif
[/source]

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

##### Share on other sites

#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:

[source]
#ifdef __cplusplus
namespace foo
{
#endif
typedef double REAL;

#ifdef __cplusplus
}
#endif
[/source]

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

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

##### 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:

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

##### Share on other sites
Make an implementation type defining the actual type of REAL, and define the REAL type as the implementation type in whatever scope you want.
[source]
#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
[/source]
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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002275
×