Linking files

This is a discussion on Linking files within the C++ Programming forums, part of the General Programming Boards category; How do you link files in MSVC++6? (i.e. I want to keep certain class declarations separate from main() for size ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    Linking files

    How do you link files in MSVC++6? (i.e. I want to keep certain class declarations separate from main() for size management purposes) and do i have to declare objects, functions and variables extern if they are defined in a separate file? if so, how?
    thanks in advance
    I AM WINNER!!!1!111oneoneomne

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Talking

    and do i have to declare objects, functions and variables extern if they are defined in a separate file? if so, how?
    I wondered this same thing a little while back.

    Here's how it works (just a dumb example):

    SomeHeader.H
    Code:
    void myFunction()
    {
    cout << "\nHello World!";
    }
    MySourceFile.CPP
    Code:
    #include <iostream.h>
    #include "SomeHeader.h"
    
    void myFunction();
    
    int main()
    {
    myFunction;
    return 0;
    }
    See the bold-italic text in the source code? It wont work without that. Even though the function is already defined in a header file which is included in the build, your program wont know what myFunction() is, or what it does. So, you must put a function prototype in there before you call it, so that your program knows what to expect when it calls the function (Hmm... was that a good way to word it?)


    Variables and other things work similarly.

    If you initialize a variable in a header file, you must write it's type into any other files that it'll be used in:

    Header1.H
    Code:
    int x = 5;
    Then, (for example) if you want to print out x's value in some other header file, you'd do the following:

    Header2.H
    Code:
    int x ;
    cout << x;
    Once again, you must include the int x; line in Header2.H, or else when you try to display x, your program wont know what/where to look for.

    Hope that helps
    Last edited by Krak; 01-14-2003 at 08:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  2. html link question about linking to files for download
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-01-2004, 01:27 PM
  3. Linking Error Whenever I Tried To Use Header Files
    By javacvb in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2003, 11:46 AM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 09:12 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21