编译具有多个文件的程序
我刚刚开始学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,您可以将两个
.cpp
文件添加到同一目标下的项目中。它的 IDE 会自动将这两个文件添加到构建中并将它们链接在一起。也就是说,Dev-C++ 非常非常古老且缺乏维护。它已经好几年没有更新了。我强烈建议您使用不同的 IDE。有很多可供选择,包括名为 wxDev-C++ 的 Dev-C++ 分支。我实际上推荐 Code::Blocks 或 Visual 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.
我不确定 Dev-C++,但概念保持不变。因此,您可以尝试让这两个文件一起工作。
如果要引用的 .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
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.
您必须使用 #include 预处理器指令包含其他文件
在文件顶部有 main() 函数的地方
例如:
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: