Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:38:40+00:00 2026-05-13T13:38:40+00:00

Instead of doing #include MyClass.cpp I would like to do #include MyClass.h I’ve read

  • 0

Instead of doing

#include "MyClass.cpp"

I would like to do

#include "MyClass.h"

I’ve read online that not doing so is considered bad practice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:38:41+00:00Added an answer on May 13, 2026 at 1:38 pm

    Separate compilation in a nutshell

    First, let’s get some quick examples out there:

    struct ClassDeclaration;   // 'class' / 'struct' mean almost the same thing here
    struct ClassDefinition {}; // the only difference is default accessibility
                               // of bases and members
    
    void function_declaration();
    void function_definition() {}
    
    extern int global_object_declaration;
    int global_object_definition;
    
    template<class T>           // cannot replace this 'class' with 'struct'
    struct ClassTemplateDeclaration;
    template<class T>
    struct ClassTemplateDefinition {};
    
    template<class T>
    void function_template_declaration();
    template<class T>
    void function_template_definition() {}
    

    Translation Unit

    A translation unit (TU) is a single source file (should be a **.cpp* file) and all the files it includes, and they include, etc. In other words: the result of preprocessing a single file.

    Headers

    Include guards are a hack to work around lack of a real module system, making headers into a kind of limited module; to this end, including the same header more than once must not have an adverse affect.

    Include guards work by making subsequent #includes no-ops, with the definitions available from the first include. Because of their limited nature, macros which control header options should be consistent throughout a project (oddball headers like <assert.h> cause problems) and all #includes of public headers should be outside of any namespace, class, etc., usually at the top of any file.

    See my include guard naming advice, including a short program to generate include guards.

    Declarations

    Classes, functions, objects, and templates may be declared almost anywhere, may be declared any number of times, and must be declared before referring to them in any way. In a few weird cases, you can declare classes as you use them; won’t cover that here.

    Definitions

    Classes may be defined at most once[1] per TU; this typically happens when you include a header for a particular class. Functions and objects must be defined once in exactly one TU; this typically happens when you implement them in a **.cpp* file. However, inline functions, including implicitly inline functions inside class definitions, may be defined in multiple TUs, but the definitions must be identical.

    For practical purposes[2], templates (both class templates and function templates) are defined only in headers, and if you want to use a separate file, then use another header[3].

    [1] Because of the at-most-once restriction, headers use include guards to prevent multiple inclusion and thus multiple definition errors.
    [2] I won’t cover the other possibilities here.
    [3] Name it blahblah_detail.hpp, blahblah_private.hpp, or similar if you want to document that it’s non-public.

    Guidelines

    So, while I’m sure everything above is all a big ball of mud so far, it’s less than a page on what should take up a few chapters, so use it as a brief reference. Understanding the concepts above, however, is important. Using those, here’s a short list of guidelines (but not absolute rules):

    • Always name headers consistently in a single project, such as **.h* for C and **.hpp* for C++.
    • Never include a file which is not a header.
    • Always name implementation files (which are going to be directly compiled) consistently, such as **.c* and **.cpp*.
    • Use a build system which can compile your source files automatically. make is the canonical example, but there are many alternatives. Keep it simple in simple cases. For example, make can be used its built-in rules and even without a makefile.
    • Use a build system which can generate header dependencies. Some compilers can generate this with command-line switches, such as -M, so you can make a surprisingly useful system easily.

    Build Process

    (Here’s the tiny bit that answers your question, but you need most of the above in order to get here.)

    When you build, the build system will then go through several steps, of which the important ones for this discussion are:

    1. compile each implementation file as a TU, producing an object file (**.o*, **.obj*)
      • each is compiled independently of the others, which is why each TU needs declarations and definitions
    2. link those files, along with libraries specified, into a single executable

    I recommend you learn the rudiments of make, as it is popular, well-understood, and easy to get started with. However, it’s an old system with several problems, and you’ll want to switch to something else at some point.

    Choosing a build system is almost a religious experience, like choosing an editor, except you’ll have to work with more people (everyone working on the same project) and will likely be much more constrained by precedent and convention. You can use an IDE which handles the same details for you, but this has no real benefit from using a comprehensive build system instead, and you really should still know what it’s doing under the hood.

    File Templates

    example.hpp

    #ifndef EXAMPLE_INCLUDE_GUARD_60497EBE580B4F5292059C8705848F75
    #define EXAMPLE_INCLUDE_GUARD_60497EBE580B4F5292059C8705848F75
    // all project-specific macros for this project are prefixed "EXAMPLE_"
    
    #include <ostream> // required headers/"modules"/libraries from the
    #include <string>  // stdlib, this project, and elsewhere
    #include <vector>
    
    namespace example { // main namespace for this project
    template<class T>
    struct TemplateExample { // for practical purposes, just put entire
      void f() {}            // definition of class and all methods in header
      T data;
    };
    
    struct FooBar {
      FooBar(); // declared
      int size() const { return v.size(); } // defined (& implicitly inline)
    private:
      std::vector<TemplateExample<int> > v;
    };
    
    int main(std::vector<std::string> args); // declared
    } // example::
    
    #endif
    

    example.cpp

    #include "example.hpp" // include the headers "specific to" this implementation
    // file first, helps make sure the header includes anything it needs (is
    // independent)
    
    #include <algorithm> // anything additional not included by the header
    #include <iostream>
    
    namespace example {
    FooBar::FooBar() : v(42) {} // define ctor
    
    int main(std::vector<std::string> args) { // define function
      using namespace std; // use inside function scope, if desired, is always okay
      // but using outside function scope can be problematic
      cout << "doing real work now...\n"; // no std:: needed here
      return 42;
    }
    } // example::
    

    main.cpp

    #include <iostream>
    #include "example.hpp"
    
    int main(int argc, char const** argv) try {
      // do any global initialization before real main
      return example::main(std::vector<std::string>(argv, argv + argc));
    }
    catch (std::exception& e) {
      std::cerr << "[uncaught exception: " << e.what() << "]\n";
      return 1; // or EXIT_FAILURE, etc.
    }
    catch (...) {
      std::cerr << "[unknown uncaught exception]\n";
      return 1; // or EXIT_FAILURE, etc.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's actually much more to it than just making the… May 14, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer I disagree with the estimable Don Stewart. Given the combinatorial… May 14, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer You accomplish this by creating a Settings.bundle in your project.… May 14, 2026 at 6:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.