Code::Blocks - 如何编译多个源文件

发布于 2024-11-06 09:34:07 字数 605 浏览 2 评论 0原文

我正在尝试编译一个具有多个源文件的程序 - 两个 CPP 文件和一个头文件,以及 code::blocks。作为示例,我创建了以下三个文件(由其他论坛上的其他人创建的示例程序):

main.cpp:

#include <stdio.h>
#include "other.h"

int main (void)
{
    printf("%d\n", getfavoritenumber());

    return 0;
}

other.cpp

#include "other.h"

int getfavoritenumber(void)
{
    return 3;
}

other.h

#ifndef _OTHER_H_
#define _OTHER_H_

int getfavoritenumber(void);

#endif

尽管事实上这三个文件应该链接当我尝试构建项目时,我收到错误“跳过链接阶段(构建目标没有要链接的目标文件)”。

我做错了什么?尝试编译各个文件会出现错误“该文件未分配给任何目标”。

I'm trying to compile a program with multiple source files - two CPP files and a header file, with code::blocks. As an example, I have created the following three files (an example program created by someone else on another forum):

main.cpp:

#include <stdio.h>
#include "other.h"

int main (void)
{
    printf("%d\n", getfavoritenumber());

    return 0;
}

other.cpp

#include "other.h"

int getfavoritenumber(void)
{
    return 3;
}

other.h

#ifndef _OTHER_H_
#define _OTHER_H_

int getfavoritenumber(void);

#endif

Despite the fact that these three files should link to each other, I receive the error "Linking stage skipped (build target has no object files to link)" when I try to build the project.

What am I doing wrong? Trying to compile the individual files presents the error "That file isn't assigned to any target".

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

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

发布评论

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

评论(6

陪你搞怪i 2024-11-13 09:34:07

以下是对我有用的方法:

转到显示项目的左侧面板,然后右键单击 .cpp 文件。选择属性,然后开始构建。选中“属于目标”标题下的框:“调试”和“发布”

Here is what worked for me:

Go to the left panel that says projects, and right-click on .cpp file. Select properties, then go to build. Check the boxes under the heading Belongs in Targets: "Debug" and "Release"

柠栀 2024-11-13 09:34:07

在创建我的第一个多源代码项目时,我遇到了类似的问题。我相信您遇到的问题不是链接,而是您的 #include 声明对我来说目录与我的预期不同。要在项目中包含头文件,我必须编写 #include "include/other.h" 看看你的文件夹系统是如何构建的......如果你可以发布哪些文件夹/目录如果你有这个项目,我也许可以给你一个更好的答案。

I had a similar problem when creating my first multi source code project. i believe the problem you are having is not with the linking but with you #include statement for me the directory's were different to what i expected. to include the header file in a project i had to write #include "include/other.h" have a look at how your folder system is constructed....if you could post what folders/directory`s you have in the project i might be able to give you a better answer.

末が日狂欢 2024-11-13 09:34:07

我遇到了类似的问题,发现如果我刚刚关闭项目,创建一个新的空白控制台应用程序,然后导入现有文件,事情就会开始编译良好。

I had a similar issue and found that if I just closed the project, created a new blank console application then imported the existing files things started to compile fine.

一花一树开 2024-11-13 09:34:07

我这样做了:

  • 我在Code::Blocks中创建了一个控制台项目

  • 对于每个文件,我使用 File|New 创建一个空文件,使用您指定的名称将其添加到项目中,并粘贴来自您的相关代码问题放入每个文件中。

  • 编译并运行生成的可执行文件。

一切都按预期进行。如果它不适合您,请描述您如何创建该项目。 Code::Blocks 绝对需要一个项目 - 它不能很好地处理单个文件。如果需要,请从命令行使用 GCC。

编辑:

  • 通常最好将编译器与 CB 分开安装,因为 CB 实际上只是一个 IDE。我假设我们这里使用的是 Windows。转至 http://tdm-gcc.tdragon.net 并从那里下载最新的 GCC 编译器。从命令行检查它是否有效。

  • 然后在 CB 中转到设置|编译器和调试器并选择工具链可执行文件选项卡。然后导航到安装 TDM GCC 内容的目录的根目录(根目录,而不是根目录中的 bin 目录),一切都应该很好。

如果最终这不起作用,请尝试 CB 支持论坛 http://forums.codeblocks.org

I did this:

  • I created a Console Project in Code::Blocks

  • For each file i did File|New to create an empty file, added it to the project with the names you specified and pasted the relevant code from your question into each file.

  • Compiled and ran the resulting executable.

Everything worked as expected. If it doesn't work for you, please describe how you are creating the project. Code::Blocks absolutely needs a project - it doesn't work well with individual files. If you want that, use GCC from the command line.

Edit:

  • It is generally a good idea to install the compiler separately from CB, which is really only an IDE. I am assuming we are on Windows here. Go to http://tdm-gcc.tdragon.net and download the latest GCC compiler from there. Check it works from the command line.

  • Then in CB go to Settings|Compiler and Debugger and select the Toolchains executables tab. Then navigate to the root of the directory where you installed the TDM GCC stuff (the root, not the bin directory within the root), and all should be well.

And if at the end of the day this doesn't work, try the CB support forums at http://forums.codeblocks.org.

秋千易 2024-11-13 09:34:07

使用 Project>Add Files... 或 Project>Add FILEs Recursively... 确保所有文件(.h 和 .cpp)已添加到项目中

Ensure all files (.h and .cpp) have been added to the project with Project>Add Files... or Project>Add FIles Recursively...

深海不蓝 2024-11-13 09:34:07

我遇到了同样的问题,您按照说明将完整的程序传输到控制台应用程序,但仍然收到错误消息。对我有用的是:应该重写程序之间共享的核心脚本(退格键,然后从输入前几个字符时出现的菜单中选择它们),这几乎就像手动指出并调用 Code::布洛克对他们共同的亲属关系的关注。哈哈。

I had the same problem, you transfer complete programs to the console application as instructed but then you still get an error message. What worked for me: the core scripts shared between the programs should be rewritten (backspace, and select them from the menu that appears when you type in the first few characters), it's almost as if its to manually point out and call Code::block's attention to their shared kinship. LOL.

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