Multifile Programming

This is a discussion on Multifile Programming within the C++ Programming forums, part of the General Programming Boards category; I am doing a multifile program. There are two files first.cpp and second.cpp that I would like to include in ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Unhappy Multifile Programming

    I am doing a multifile program. There are two files first.cpp and second.cpp that I would like to include in the main file main.cpp. I have first.h and second.h, that I include using
    Code:
    // in file main.cpp
    #include "first.h"
    #include "second.h"
    The .h files look something like this:
    Code:
    // in files first.h and second.h
    #ifndef __FIRST_H
    #define __SECOND_H
    
    int	init(void);
    
    #endif
    And in the files first.cpp and second.cpp, I have added the lines
    Code:
    // in files first.cpp and second.cpp
    #include "first.h"
    #include "second.h"
    I use the Turbo C++ 3.0 compiler. I have set the include directory to the directory that contains the .h and .cpp files. I can compile successfully, but I can't link it. The error message is 'Undefined symbol init() in module main.cpp'. Please help.
    Last edited by z0diac; 11-28-2003 at 09:32 AM.

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Put your header files into the directory you are compiling from.
    Do not make direct eye contact with me.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    first.h:

    Code:
    #ifndef FIRST_H
    #define FIRST_H
    void first(void);
    #endif // FIRST_H

    second.h:

    Code:
    #ifndef SECOND_H
    #define SECOND_H
    void second(void);
    #endif // SECOND_HS

    main.h:

    Code:
    #ifndef MAIN_H
    #define MAIN_H
    #include "first.h"
    #include "second.h"
    #endif //MAIN_H

    first.cpp:

    Code:
    #include "first.h"
    #include <iostream>
    void first(void)
    {
     std::cout << "First" << std::endl;
    }

    second.cpp

    Code:
    #include "second.h"
    #include <iostream>
    void second(void)
    {
     std::cout << "Second" << std::endl;
    }

    main.cpp

    Code:
    #include "main.h"
    int main()
    {
     first();
     second();
    }



    First, compile first.cpp and second.cpp.

    Then compile main.cpp, passing first.o and second.o as additional arguments to the command line (read your compiler's documentation for details on how the command line input should be formatted).

    That's basically it.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Smile Thanks

    Thanks a lot, Sebastiani! Your solution saved me. Thanks goes to Lurker also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Name disamb. in multifile project
    By gibbofresco in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 11:15 AM
  2. Template + Multifile
    By Mehdi in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2006, 06:17 AM
  3. Compiling Multifile Program
    By tdk_ratboy in forum C++ Programming
    Replies: 7
    Last Post: 06-04-2004, 02:36 PM

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