Thread: Get sense of internal linkage and external linkage

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    Get sense of internal linkage and external linkage

    I have learned C for months and now I want to learn C++. I usually learned C by reading and compiling only one souce file. I have no experience compiling multiple source file. When I learn C++ tutorial, I confuse with linkage concept. I have read their definition. The definition is clear but my mind is not. I just want get sense of linkage concept by compiling the source files. I want to get the example. So could you give me a simple example to get sense of internal linkage and external linkage especially related with const and inline functions concept???

    I am new for this board. Is this question suitable for this forum?? I have searced with the search engine in this forum without any helping result.

    Thanx.

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Took me ages to get a handle on this.

    Say you have a main file and you want to use a function that prints a string:
    Code:
    //  main.cpp
    #include <string>
    #include "MyHeader.h"
    using namespace std;
    
    int main()
    {
         string Welcome("Welcome to my program!");
    
         PrintString(Welcome);
    
         return 0;
    }
    Then you have a header file, which provides the link for the compiler to the PrintString() function.
    Code:
    //  MyHeader.h
    #ifndef MYHEADER_H
    #define MYHEADER_H
    
    #include <string>
    using namespace std;
    
    void PrintString(string ToPrint);
    
    #endif
    Then you have the .cpp file that contains the code to PrintString()
    Code:
    //  PrintStr.cpp
    #include <string>
    #include <iostream>
    #include "MyHeader.h"
    using namespace std;
    
    void PrintString(string ToPrint)
    {
         cout << ToPrint;
    }
    The #ifndef and #define ensure that the file isn't included multiple times per file (i.e some other include also includes this header).
    edit: Like how my .cpps include <string> but MyHeader also includes <string>
    Last edited by HybridM; 10-14-2003 at 06:02 AM.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. external linkage in a block?
    By password636 in forum C Programming
    Replies: 5
    Last Post: 07-07-2009, 10:29 AM
  2. Linkage question
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2009, 07:56 PM
  3. internal/external linkage
    By MarkZWEERS in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2008, 03:34 AM
  4. linkage query
    By c_cool in forum C Programming
    Replies: 1
    Last Post: 09-11-2004, 02:36 AM