Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to force compile C source by C++ compiler?
Forum Updated to NodeBB v4.3 + New Features

How to force compile C source by C++ compiler?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
6 Posts 4 Posters 6.9k Views 2 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.
  • F Offline
    F Offline
    fsb4000
    wrote on last edited by
    #1

    I am writing an application in C subset of C ++ and use Qt Creator as an IDE. I would like to know how to make Qt Creator to compile C code with a C ++ compiler.
    Thanks in advance for answers!

    raven-worxR jsulmJ 2 Replies Last reply
    0
    • F fsb4000

      I am writing an application in C subset of C ++ and use Qt Creator as an IDE. I would like to know how to make Qt Creator to compile C code with a C ++ compiler.
      Thanks in advance for answers!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @fsb4000
      AFAIK most compilers do that automatically when the file has a .c extension?
      Otherwise you would need to specify the corresponding flag for your compiler. Unfortunately this can't be done straight forward using qmake.

      --- 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
      • F fsb4000

        I am writing an application in C subset of C ++ and use Qt Creator as an IDE. I would like to know how to make Qt Creator to compile C code with a C ++ compiler.
        Thanks in advance for answers!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @fsb4000 If you have a C++ compiler then you should have C compiler as well. Which compiler do you have? What file name extension do you use for your source code files (you should use .c for C)?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fsb4000
          wrote on last edited by fsb4000
          #4

          Sorry for bad english, I think I do not accurately express.

          1. I am writing an application in C subset of C ++ and use Qt Creator as an IDE.
          2. Now it works like this:
          gcc -c <some flags> main.o main.c
          gcc  -o <app_name> main.o ....
          
          1. I want to test that my code works with C++ compiler. So I can test both C and C++ support. So I want similar to this:
          contains(CPP, 1):{
              QMAKE_CC = QMAKE_CXX
              QMAKE_CFLAGS = QMAKE_CXXFLAGS
          } else {
              CONFIG += use_c_linker
          }
          

          It does not work, but it works like this:

          contains(CPP, 1):{
              QMAKE_CC = g++
              QMAKE_CFLAGS = -std=c++14 <other flags>
          } else {
              CONFIG += use_c_linker
          }
          

          But I want this to work at least with these three compilers: g++, clang, MSVC, not only g++.

          JohanSoloJ 1 Reply Last reply
          0
          • F fsb4000

            Sorry for bad english, I think I do not accurately express.

            1. I am writing an application in C subset of C ++ and use Qt Creator as an IDE.
            2. Now it works like this:
            gcc -c <some flags> main.o main.c
            gcc  -o <app_name> main.o ....
            
            1. I want to test that my code works with C++ compiler. So I can test both C and C++ support. So I want similar to this:
            contains(CPP, 1):{
                QMAKE_CC = QMAKE_CXX
                QMAKE_CFLAGS = QMAKE_CXXFLAGS
            } else {
                CONFIG += use_c_linker
            }
            

            It does not work, but it works like this:

            contains(CPP, 1):{
                QMAKE_CC = g++
                QMAKE_CFLAGS = -std=c++14 <other flags>
            } else {
                CONFIG += use_c_linker
            }
            

            But I want this to work at least with these three compilers: g++, clang, MSVC, not only g++.

            JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5

            @fsb4000 said in How to force compile C source by C++ compiler?:

            But I want this to work at least with these three compilers: g++, clang, MSVC, not only g++.

            IIRC MSVC does not have any more a pure C compiler.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fsb4000
              wrote on last edited by fsb4000
              #6

              I figured out.
              Solution::

              contains(CPP, 1):{
                  QMAKE_CC = $$QMAKE_CXX
                  QMAKE_CFLAGS  = $$QMAKE_CXXFLAGS
              } else {
                  CONFIG += use_c_linker
              }
              

              Thank you all for for your time!

              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