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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:39:26+00:00 2026-05-29T10:39:26+00:00

How does the main function know about function definitions (implementations) in a different file?

  • 0

How does the main function know about function definitions (implementations) in a different file?

For example, say I have 3 files:

//main.cpp

#include "myfunction.hpp"
int main() {
    int A = myfunction( 12 );
    ...
}
//myfunction.cpp

#include "myfunction.hpp"
int myfunction( int x ) {
    return x * x;
}
//myfunction.hpp
    
int myfunction( int x );

I get how the preprocessor includes the header code, but how do the header and main function even know the function definition exists, much less utilize it?

  • 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-29T10:39:27+00:00Added an answer on May 29, 2026 at 10:39 am

    The header file declares functions/classes – i.e. tells the compiler when it is compiling a .cpp file what functions/classes are available.

    The .cpp file defines those functions – i.e. the compiler compiles the code and therefore produces the actual machine code to perform those actions that are declared in the corresponding .hpp file.

    In your example, main.cpp includes a .hpp file. The preprocessor replaces the #include with the contents of the .hpp file. This file tells the compiler that the function myfunction is defined elsewhere and it takes one parameter (an int) and returns an int.

    So when you compile main.cpp into object file (.o extension) it makes a note in that file that it requires the function myfunction. When you compile myfunction.cpp into an object file, the object file has a note in it that it has the definition for myfunction.

    Then when you come to linking the two object files together into an executable, the linker ties the ends up – i.e. main.o uses myfunction as defined in myfunction.o.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the main function we define in C or C++ run in a process
On my main page I have this jquery code which does an ajax call
im just curious about the following example #include<stdio.h> int test(); int test(){ // int
Does anyone know a good document/article about GDI resource handling? I need to share
Does this code cause a memory leak: int main(){ int * a = new
does anybody know what happend to http://www.sics.se/~adam/uip/index.php/Main_Page ? the complete sics.se server appears to
Why does the following code seg fault before returning: int main() { char iD[20];
The following code does not compile: public class GenericsTest { public static void main(String[]
Why does the following program segfault? int main() { main(); } Even though it
main() { printf(Hello World.); } Why does no warning is produced in gcc compiler

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.