Qt World Summit: Register Today!

Treat .c files as c++

  • My project uses .c extension for c++ files. I know that's yucky but I can't change it.
    I upgraded Qt Creator from 4.0.1., which treated .c as c++ to 4.6.1, which doesn't. Is it possible somehow to treat .c files as c++ in 4.6.1?

  • @eugcomax

    Hi and welcome to devnet forum

    This is merely a handling question of the compiler. Qt creator, and version 4.6.1 is the latest, is simply an IDE (integrated Development Environment).

    When you rename all source files fromextension .c to .cpp the compiler will handle those as c++.

    Other possibilities are coming through the compiler you are using, when the compiler supports it.

    However, you would need to publish the OS and compiler you are using.

  • Thanks for a quick answer. I don't intend to compile in the Qt creator. 4.0.1 successfully parses my code while 4.6.1. not. I use centos 7 and gcc 4.9.2. I tried to change the compiler to clang 3.8 but that didn't help.

  • @eugcomax

    Probably the second paragraph is what you need from here

    In Qt creator go to "Tools"->Options"->"Build&Run". Under "Compilers" there are typically entries for C and C++ and the actual compiler call is defined. Try to add a new entry for C and use the compiler statement as found under C++. Later you need to make sure that this is used in your toolchain for compilation.

    Note: never done this before, therefore, you may have to check that is does work properly.

  • @koahnig , I have the same issue.

    It's not a problem of build system or a problem of selected "Kit". But it is seems to be a problem of Creator's feature called Code Model.

    Here is my use case:

    1. Create a valid C++ project with any build system. Place there some files with .c and .cpp extensions.
    2. Import the project to Qt ( File->New File or Project->Import Project->Import Existing Project
      2.1 Navigate to the project's folder and choose it.
      2.2. Select all the source files. Click 'Next', click 'Finish'. Wizard closes, Qt Creator starts indexing files and builds its code model.
    3. Open any .c file which contains specific C++ only lines. Lines are not highlighted properly, navigation and refactoring features doesn't work.
    4. Go to Tools->C++->Inspect C++ Code Model
      4.1 See two "Project Parts". View both of them.
      4.2 One of them has "C11" value at "Language Version" key.

    Code model can't be changed using GUI and I din't find any way to manipulate it in other ways (I reviewed Qt Creator configs(...AppData\Roaming\QtProject) and project files (*.creator, *.creator.user).
    Tried 4.6.1 and 4.7.0 Beta 1 versions.

    Can anybody help me?

  • @vaselo

    If you are positive that this is a bug, you can check always on JIRA for bug report or file one on your own.

    Especially when you have found already procedures to verify and show the problem, the best place is JIRA. That is the only place where bug reports are handled properly. The forum here is not monitored for eventual bugs and their reports.

  • I've found a way!
    I analyzed Creator's sources and saw that project parts are bound to MIME type.
    To treat .c files as C++ you only need to edit MIME settings:

    1. In main menu go to Tools->Options->Environment->Mime Types
    2. Edit "text/x-c++src" entry and modify its pattern to match .c files
    3. Edit "text/x-csr" and clear it's pattern.

    Now you'll get a single project part and all the Code Model will treat files as valid C++ files!

Log in to reply