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



  • 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!


  • Moderators

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


  • Moderators

    @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)?



  • 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++.



  • @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.



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.