Compiling a program with multiple files

13,962

Solution 1

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.

Solution 2

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.

Solution 3

#include "path/filename.c"

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

Solution 4

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 */
...
Share:
13,962
Greener
Author by

Greener

Updated on June 07, 2022

Comments

  • Greener
    Greener almost 2 years

    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.

  • Greener
    Greener almost 13 years
    Thanks. I figured out how to do it but this helps reinforce my understanding of the process. I just forgot I needed to forward declare the function so it would compile.
  • Greener
    Greener almost 13 years
    Thanks for the information. I heard Dev-C++ was basically dropped but I didn't know that wxDev-C++ was the continuation; I thought someone else picked it up. To be honest I didn't really like a lot of things about it anyway.
  • Daniel R. Collins
    Daniel R. Collins over 6 years
    Late comment: As of this date (2017), wxDev-C++ has itself not been updated in about 6 years (last update 2011; last reported bugs 2012). On the other hand, another fork of Dev-C++ by Orwell was released 2 years ago (2015).
  • LordWilmore
    LordWilmore over 6 years
    Can you please add some explanation around this, and possible (given the level of question being asked), use correct syntax for the declaration of main?
  • Hans Olsson
    Hans Olsson over 6 years
    Apart from syntax issues and lack of explanation the idea with having multiple files is generally to compile them separately. This answer just includes them together which is generally a bad idea.