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 6214419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:52:23+00:00 2026-05-24T06:52:23+00:00

I have a question about source file and header in c++. I have a

  • 0

I have a question about source file and header in c++.

I have a header which is used to declare the functions. And I implement these functions in C++.

Now I need to use these functions in other files. Should I include both source file and header file in order to use these functions?

  • 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-24T06:52:25+00:00Added an answer on May 24, 2026 at 6:52 am

    The basic compilation model used by (C and) C++ is thus:

    1. You put declarations that are to be shared between source files into header files. The source files that need to access those declarations then include these header files.
    2. When compiling, the preprocessor will (recursively) replace #include directives with the file(s) included. The result is called a compilation unit.
    3. The compiler then compiles these compilation units, one at a time, into object files. (Note: The compiler always only ever sees a single translation unit. It has no access to whatever is declared and defined in other translation units.) Basically, object files are readily compiled code, but all the references to symbols (functions, variables) outside of their compilation unit are still symbolic.
    4. The linker is then passed all object files and links references to symbols across those object files to the symbols’ definitions, spitting out an executable if all goes well.

    In C++, this is often a bit more complicated in practice (especially due to inlining and templates, but also with features like link-time code generation), but that’s the basic principles.

    The implications of that are:

    • Source files usually only include header files.
      Since the whole preprocessor magic is just a simple text replacement engine without any knowledge of (C or) C++ and about its purpose in the process described above, it can be bend and and abused to do other things. In this, including source files has been done sometimes to achieve some goal. But it’s rare.
    • Symbols can be declared as often as wanted, but must be defined exactly once.
      If a linker can’t find a symbol that has been declared (and thus references to it have been accepted by the compiler), it will spit a nasty error message into your face. If it finds multiple definitions it will do likewise.
    • The compiler doesn’t care whether a declaration is coming from a header included by the preprocessor or has been written directly into the source file.
      However, if you write declarations directly into source files, the compiler, not being able to “look” into other translation units, can’t warn you that they are out of date. If you put declarations into header files instead, it is much easier to keep them in sync with their corresponding definitions, and often the compiler can even diagnose if they don’t match.
    • Any project with more than one source file can only be built by several compilation runs (one for each source file) with the linker linking the resulting object files. Often though, IDEs hide that behind their project management.
      If you change a header that is, directly or indirectly, used in many source files, you will have to re-compile most of your project. If you change a source file, you will only have to re-compile that one source files (and re-link the executable, of course).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about design winforms. Should I use, or not, group boxes
I've got a question about use of permissive-licensed (BSD, Apache, MIT, etc) source where
My question is about memory use and objects in actionscript 2. If I have
I have a question about using #undef to redefine macros. I have a file
I have a question about the images displaying using a function getImage_w($image,$dst_w), which takes
I have a question about package in TCL I have a test1.tcl file: package
Quick question about MSBuild. I have the following MSBuild file in directory d:\MyDirectory <Project
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex

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.