开发者

Compiling a program with multiple files

I just started learning C++ with Dev C++ as my IDE. One of the tutorials I'm using has a page in it about comp开发者_开发技巧iling 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.


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.


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.


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 */
...


#include "path/filename.c"

main
{
...
...
...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜