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

  • Home
  • SEARCH
  • 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 3966960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:39:48+00:00 2026-05-20T03:39:48+00:00

What is the difference between linking to include files versus linking to lib files?

  • 0

What is the difference between linking to include files versus linking to lib files?

I am fairly new to C/C++ and I’m having a hard time figuring out the difference between using include files and a static lib file to call functions. In my mind, the include files have functions that one can call just like .lib files.

  • 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-20T03:39:49+00:00Added an answer on May 20, 2026 at 3:39 am

    In C++ (and C and other similar languages) a function is said to have both a declaration and a definition.

    The declaration is simply a short statement that declares that the function exists, and what its interface looks like. Consider a basic function add that adds two integers together. Its declaration might look like the following:

    int add(int, int);
    

    This means “there exists a function add that takes two integers and returns an integer”. It does not specify what the function actually does, despite the fact that we can make a good guess based on its name.

    The definition of the function is where we define exactly what the function does. This might be what you consider to be the actual function code. Using the add function as an example:

    int add (int a, int b)
    {
        return a + b;
    }
    

    So how does this fit with your question? Well, suppose we have a number of math functions in math.cpp:

    // math.cpp
    
    int add (int a, int b)
    {
        return a + b;
    }
    
    int sub(int a, int b)
    {
        return a - b;
    }
    

    And also suppose we decide to use some of these in our main function in main.cpp:

    // main.cpp
    
    #include <iostream>
    
    int main (int argc, char* argv[])
    {
        std::cout << "1 + 2 = " << add(1, 2) << std::endl;
        std::cout << "8 - 3 = " << sub(8, 3) << std::endl;
    }
    

    If you try to compile main.cpp as it is, it will complain that it doesn’t know what add and sub are. This is because you are trying to use them without declaring that they exist – which is exactly what a declaration is for. So you might do the following:

    // main.cpp
    
    #include <iostream>
    
    int add(int, int);
    int sub(int, int);
    
    int main (int argc, char* argv[])
    {
        std::cout << "1 + 2 = " << add(1, 2) << std::endl;
        std::cout << "8 - 3 = " << sub(8, 3) << std::endl;
    }
    

    This would work, but is not very flexible. If we add a new function mul, we need to go and add its declaration to main.cpp and every other .cpp file that uses it (which is a lot of work if you have a lot of .cpp files). So what we do is instead we put all the declarations into a single file (say, math.h) so we only have to maintain the list of declarations in a single spot. Then, we simply include math.h into any file that uses the math functions. This is the purpose of header files (a.k.a. include files).

    This works great, but could be even better. As it is, we have a main.cpp file, and a math.cpp file, both of which are compiled every time you compile the program*. If your math functions don’t change at all, surely it’s better to compile them once and just insert the pre-compiled definitions into your executable whenever you recompile main.cpp? That is exactly the purpose of .lib files. They contain the pre-compiled code for definitions of the relevant functions. You still need the include file to let you know what functions exist in the lib.

    The purpose of the linking stage of compilation is to take these pre-compiled functions and the functions you have just compiled, and roll them together into a single executable file.

    Essentially, you can look at a static lib as the pre-compiled code for a number of predefined functions, and its matching include file as a tool to let any code wanting to use those functions know which are available and what their description is.


    * This is not strictly true, but is sufficient for our purposes here.

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

Sidebar

Related Questions

No related questions found

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.