Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeBlocks, GCC: change project language c and c++?

When I select console project to start with, it lets you to select C or C++. But once its created, I can't figure out how to change it. Plus, when you create a Win32 GUI application, it doesn't give you the option at all and its default is C++.

Where can I change to C? I have been looking in all the project settings for ages. Renaming my file from .cpp to .c doesn't seem to do anything, it compiles the file as C++. I know that without the IDE, you just change your executable from g++ to gcc, but how do I set this for the current project in CodeBlocks?

like image 652
Kaije Avatar asked Jan 14 '11 21:01

Kaije


1 Answers

The only tangible difference between selecting C vs C++ when you create a project is which compiler is invoked for the translation units during a build. Code::Blocks currently does not provide a way to directly change this after project creation. That is to say you would have to change each source file one at a time to get what you want.

Here's what you can do to change it:

  • Open the properties window for a source you want to change. You can get to it by right-click source file->properties.

    Right click on the source file and go to "Properties..."

  • Goto the Advanced tab.
  • Find the Compiler variable field and change it from CPP to CC.

    Change the compiler variable from <code>CPP</code> to <code>CC</code>

  • Click Ok.
  • Repeat this for each source file that needs to be changed.

Now if your existing project contains a lot of source files you can do this quicker by manually editing the Code::Blocks .cbp project file (it's just an XML file). The nodes you want to search for and replace will look something like this:

<CodeBlocks_project_file>
    <!-- ... -->
    <Project>
        <!-- ... -->

        <Unit filename="source1.cpp">
            <Option compilerVar="CPP" />  <!-- Change CPP to CC here -->
        </Unit>
        <Unit filename="source2.cpp">
            <Option compilerVar="CPP" />  <!-- And here -->
        </Unit>
        <Unit filename="source3.cpp">
            <Option compilerVar="CPP" /> <!-- And here then save. -->
        </Unit>
        <!-- ... -->
    </Project>
</CodeBlocks_project_file>

After the changes, open your project in Code::Blocks and confirm it's being compiled as a C source file. You should see the build log invoking gcc now instead of g++.

like image 57
greatwolf Avatar answered Nov 04 '22 11:11

greatwolf