编译具有多个文件的程序

发布于 2024-11-06 18:30:52 字数 227 浏览 3 评论 0原文

我刚刚开始学习 C++,使用 Dev C++ 作为我的 IDE。我正在使用的教程中有一个页面是关于编译由多个文件组成的程序的。此时很简单,我有一个文件中包含一个函数,另一个文件包含调用该函数并输出结果所需的所有其他代码。问题是,教程没有告诉我如何加入这些文件,以便我可以编译程序并使其工作。似乎有多种方法可以做到这一点,我都想要它们,但我现在主要寻找最简单的一种。

我还应该提到,我对此很陌生,所以请尽量让您的解释简单易懂。

I just started learning C++ with Dev C++ as my IDE. One of the tutorials I'm using has a page in it about compiling a program made up of multiple files. It's simple stuff at this point, I have one file with a function in it, and the other file has all the other required code to call the function and output the results. The problem is that the tutorial doesn't tell me how to join these files so I can compile the program and have it work. There's seems to be multiple ways of doing this and I'd like them all but I'm mainly look for the simplest one right now.

I should also mention that I'm new at this so please try and keep your explanations simple and understandable.

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

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

发布评论

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

评论(4

以往的大感动 2024-11-13 18:30:52

通常,您可以将两个 .cpp 文件添加到同一目标下的项目中。它的 IDE 会自动将这两个文件添加到构建中并将它们链接在一起。


也就是说,Dev-C++ 非常非常古老且缺乏维护。它已经好几年没有更新了。我强烈建议您使用不同的 IDE。有很多可供选择,包括名为 wxDev-C++ 的 Dev-C++ 分支。我实际上推荐 Code::BlocksVisual Studio Express,它们更加现代,并且对调试和许多其他功能有更好的支持。

In general, you would add both .cpp files to your project under the same target. It IDE will automatically add both files to the build and link them together.


That said, Dev-C++ is very, very old and unmaintained. It has not seen updates in several years. I strongly urge you to use a different IDE. There are many to choose from, including a fork of Dev-C++ called wxDev-C++. I'd actually recommend Code::Blocks or Visual Studio Express, which are both much more modern and have better support for debugging and many other features.

东风软 2024-11-13 18:30:52

我不确定 Dev-C++,但概念保持不变。因此,您可以尝试让这两个文件一起工作。

  1. 每个 C++ 文件都是一个编译单元 - 这意味着编译器会将一个 .cpp / .cxx 文件转换为一个 .obj / .o 文件(在 Windows 和 Linux 上) (或任何 Unix))分别
  2. obj 文件,称为目标文件,包含该特定文件中存在的类和函数的机器代码(这里跳过一些内部细节)
  3. 如果您想访问不同编译单元中存在的函数,您需要链接这两个目标文件
    • 链接是一个术语,用于链接两个目标文件
    • 有一个单独的进程(编译器除外)负责目标文件的链接
  4. 因此,在您的情况下,您需要使用 dev -c++ 编译器并创建单独的目标文件
  5. 然后使用链接器链接两个目标文件以创建最终的可执行文件

如果要引用的 .cpp 文件中存在函数,则可以使用头文件。头文件包含函数/类声明。 .cpp 文件将包含实现。因此,在您的 .cpp 文件之一(例如 A.cpp)中,包含标头 B.hpp 并使用 B.hpp 文件中的函数。包含标头将告诉编译器函数声明存在于其他地方,并且链接器将负责将所有这些引用串在一起以创建最终的可执行文件。

希望这有帮助,否则,请随时提及您正在使用的文件,我可以建议如何将两个 .cpp 文件链接在一起。

I am not sure of Dev-C++, but the concepts remain the same. So, here is how you can try to get both the files to work together

  1. Each C++ file is a compilation unit - meaning, the compiler will convert one .cpp / .cxx file to one .obj / .o file (on Windows and Linux (or any Unix)) respectively
  2. The obj files, called the object files contain the machine code (am skipping few internal details here) for the classes and functions present in that particular file
  3. If you want to access the functions present in a different compilation unit, you need to link those two object files
    • Linking is a term that is used to, well, link two object files
    • There is a separate process (other than the compiler) which does the linking of the object files
  4. So,in your case, you need to use the dev-c++ compiler and create separate object files
  5. Then using the linker you link both the object files to create the final executable

If there are functions that exist in the .cpp files that you want to reference, you use the header files. The header files contain the function/class declarations. The .cpp files will have the implementations. So, in one of your .cpp file, (say) A.cpp, you include the header B.hpp and use the functions in the B.hpp file. The inclusion of headers will tell the compiler that the function declarations exist elsewhere and that the linker will take care of stringing all these references together to create the final executable.

Hope this helps, else, please don't hesitate to mention the files you are using and I can suggest how to link both the .cpp files together.

浮华 2024-11-13 18:30:52

您必须使用 #include 预处理器指令包含其他文件
在文件顶部有 main() 函数的地方

例如:

#include "filename.h"
...
/* rest of code containing main function goes here */
...

You must include the other files by using the #include preprocessor directive
in the top of the file where you have the main() function

For example:

#include "filename.h"
...
/* rest of code containing main function goes here */
...
貪欢 2024-11-13 18:30:52
#include "path/filename.c"

main
{
...
...
...
}
#include "path/filename.c"

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