Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mixing C++ and C Code; the define __cplusplus
Forum Updated to NodeBB v4.3 + New Features

Mixing C++ and C Code; the define __cplusplus

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M4chin3
    wrote on last edited by
    #1

    Hi,

    I'm working on a project which is mixing C++ and C code. The modules communicate with each other by function calls. Therefore I'm using extern "C" {} within the cpp header files and including c header files within the brackets which works great.

    Now I'm facing the following problem:

    Within cpp files __cplusplus is defined. The following code:
    @
    #ifdef __cplusplus
    #error __cplusplus is defined!
    #else
    #error __cplusplus is not defined!
    #endif
    @

    puts out: __cplusplus is defined!

    But within c files __cplusplus is not defined.
    The code above puts out: __cplusplus is not defined!

    I would like to have __cplusplus also defined for the precompilation of the c code files because I need to hide some #pragma instructions which cannot be processed by the c++ compiler.

    Any ideas?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      by default most compilers are guessing they C/C++ compile mode by the file extension.
      So they are also providing compiler specific flags to force them into a specific mode.

      e.g. for "MSVC ":https://msdn.microsoft.com/en-us/library/032xwy55.aspxcompilers

      Is the C lib under your control or is it a 3rd party library?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        M4chin3
        wrote on last edited by
        #3

        Okay, so it seems that in my case the compiler/precompiler determines depending on the file extension (.c/.cpp) which language is used and therefore defines __cplusplus or not.

        I'm using Qt 5.3.2 with MinGW (so it's the GCC). I will look for the flags you mentioned.

        Thanks!

        edit:
        The C lib is partly under my control, this means I want to change as less as possible and the __cplusplus define seems to be a good compromise. If it's possible I would like to stick to it. Instead of creating a own define like __cplusplus1 or smth else

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved