windows CodeBlocks、GCC:更改项目语言 c 和 c++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4695896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:59:02  来源:igfitidea点击:

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

c++cwindowscodeblocks

提问by Kaije

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

当我选择控制台项目开始时,它可以让您选择 C ​​或 C++。但是一旦创建,我无法弄清楚如何更改它。另外,当您创建 Win32 GUI 应用程序时,它根本没有给您选项,它的默认值是 C++。

Where can I change to C? I have been looking in all the project settings for ages. Renaming my file from .cppto .cdoesn'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?

哪里可以改成C?我一直在寻找所有项目设置。将我的文件从.cppto重命名.c似乎没有任何作用,它将文件编译为 C++。我知道如果没有 IDE,您只需将可执行文件从 更改g++gcc,但是如何为 CodeBlocks 中的当前项目设置它?

回答by greatwolf

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.

创建项目时选择 C ​​与 C++ 之间的唯一明显区别是在构建期间为翻译单元调用哪个编译器。Code::Blocks 目前不提供在项目创建后直接更改它的方法。也就是说,您必须一次更改每个源文件才能获得所需的内容。

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 CPPto 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.
  • 打开要更改的源的属性窗口。您可以通过右键单击源文件-> 属性来访问它。

    右键单击源文件并转到“属性...”

  • 转到高级选项卡。
  • 找到 Compiler 变量字段并将其从 更改CPPCC

    将编译器变量从 <code>CPP</code>更改为 <code>CC</code>

  • 单击确定。
  • 对每个需要更改的源文件重复此操作。

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

现在,如果您现有的项目包含大量源文件,您可以通过手动编辑 Code::Blocks.cbp项目文件(它只是一个 XML 文件)来更快地完成此操作。您要搜索和替换的节点将如下所示:

<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 gccnow instead of g++.

更改后,在 Code::Blocks 中打开您的项目并确认它正在编译为 C 源文件。您gcc现在应该看到构建日志调用而不是g++.