Thread: Multifile Programming

  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 10: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,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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, 07:17 AM
  3. Compiling Multifile Program
    By tdk_ratboy in forum C++ Programming
    Replies: 7
    Last Post: 06-04-2004, 02:36 PM