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

发布于 2024-10-12 06:16:41 字数 288 浏览 5 评论 0原文

当我选择开始控制台项目时,它允许您选择 C ​​或 C++。但一旦创建,我不知道如何更改它。另外,当您创建 Win32 GUI 应用程序时,它根本不提供该选项,并且其默认值为 C++。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尸血腥色 2024-10-19 06:16:41

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

您可以执行以下操作来更改它:

  • 打开要更改的源的属性窗口。您可以通过右键单击源文件->属性来访问它。

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

  • 转到“高级”选项卡。
  • 找到编译器变量字段并将其从 CPP 更改为 CC

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

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

现在,如果您现有的项目包含大量源文件,您可以通过手动编辑 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>

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文