PDA

View Full Version : Mixing C++ and C Code; the define __cplusplus



M4chin3
26th January 2015, 09:49
Hi,

I’m working on a project which is mixing C++ and C code. I’m now facing the problem that the C code uses #pragma instructions the compiler does not understand. I want to hide those using #ifndef __cplusplus but this define is only available when compiling .cpp files. While .c files are compiled this define is not set since the C compiler is being used.

Any ideas how I can set this define for the C compiler manually without interfeering with the C++ complier?

Using


DEFINES += "define"

within the .pro file will result in errors since the C++ compiler also defines this for himself.


Using:
Win7 64bit
Qt 5.3.2
MinGW, GCC 4.8.2

anda_skoa
26th January 2015, 13:39
That sounds a bit contradictory.

The C compiler is being used for .c files, but those .c files contain #pragma instructions that the C compiler does not understand?
Why do these files then have these instructions?

Maybe you can post some simple example?

Cheers,
_

M4chin3
26th January 2015, 14:37
Maybe you can post some simple example?
_

It's pretty easy:
The C compiler does not understand the dialect of the used C language since the program is written for microcontrollers.
The #pragma directives I'm worring about are putting variables in certain RAM sectors (a normal thing on µCs) but this is not possible on a PC since we are using the virtual address range.

I found a solution to my problem but this does not solve the main problem:



DEFINES *= "__cplusplus"


This adds __cplusplus only if it is not yet existing within the configuration.

Now I'm facing other problems that made clear I should not do this (define this manually).
I'm now trying to force qmake/GCC to use the C++ compiler for all files (no matter if .c or .cpp) and to add extern "C" to every .c file since it seems to be the right way to do it.

Any ideas how I get qmake to force using the C++ compiler?

Added after 39 minutes:

I got a solution now which was my backup solution at the beginning but I didn't want to use it since it didn't seem "right".
But in the end it turns out it is the only right solution:

I'm now using a own define for the PC program which is defined for the whole project via DEFINES += "mydefine". And I'm using this to hide the #pragma directives.
It just doesn't seem right either to tell the C compiler it would be a C++ compiler by adding manually the __cplusplus define (it also doesn't work because it starts seeing C++ code which it cannot compile) or compiling C code with a C++ compiler (because it is frigging C code and not C++).

anda_skoa
26th January 2015, 14:50
It's pretty easy:
The C compiler does not understand the dialect of the used C language since the program is written for microcontrollers.
The #pragma directives I'm worring about are putting variables in certain RAM sectors (a normal thing on µCs) but this is not possible on a PC since we are using the virtual address range.

So it is not a C++ or C compiler thing but something that depends on the platform.

You could guard the microcontroller specific pieces with a guard that is only true when building for the microcontroller or the other way around.
Most platforms even have specific platform guards for that very purpose.

Cheers,
_

M4chin3
26th January 2015, 16:11
So it is not a C++ or C compiler thing but something that depends on the platform.

You could guard the microcontroller specific pieces with a guard that is only true when building for the microcontroller or the other way around.
Most platforms even have specific platform guards for that very purpose.

Cheers,
_

That's exactly what I will do now. Now with a define specified by me to go on, later I will find out which one is the correct target platform.

Thanks!