Results 1 to 5 of 5

Thread: Mixing C++ and C Code; the define __cplusplus

  1. #1
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Question Mixing C++ and C Code; the define __cplusplus

    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

    Qt Code:
    1. DEFINES += "define"
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C++ and C Code; the define __cplusplus

    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,
    _

  3. #3
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: Mixing C++ and C Code; the define __cplusplus

    Quote Originally Posted by anda_skoa View Post
    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:

    Qt Code:
    1. DEFINES *= "__cplusplus"
    To copy to clipboard, switch view to plain text mode 

    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++).
    Last edited by M4chin3; 26th January 2015 at 13:37.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C++ and C Code; the define __cplusplus

    Quote Originally Posted by M4chin3 View Post
    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,
    _

  5. #5
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: Mixing C++ and C Code; the define __cplusplus

    Quote Originally Posted by anda_skoa View Post
    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!

Similar Threads

  1. mixing qt and mfc
    By marco.stanzani in forum Qt Programming
    Replies: 4
    Last Post: 30th December 2012, 17:30
  2. Mixing Qt and c
    By Aladin in forum General Programming
    Replies: 8
    Last Post: 28th March 2011, 14:29
  3. __cplusplus undefined on Symbian
    By bender86 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 23rd December 2010, 19:43
  4. Mixing MFC and Qt
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2006, 14:55
  5. Mixing C and Qt
    By forloRn_ in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 00:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.