Thread: How to use multiple source files?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    How to use multiple source files?

    I have an abstratc base class and two other classes, now how do I make this work?
    I would like to have one classes functions and the other one in seperate source files.

    I tried putting each class in it's own header, and the two other headers containing classes in the first header. But the error i get that the base class isn't defined.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post the smallest program that illustrates your problem. Have you included you header file for your base class in your derived class header files?

    Jim

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also mention what compiler or IDE monstronsity you are using.

    Generally includes work this way: For each class, you have an .h and a .cpp. The class declaration is in the .h, method definitions in the .cpp. The .cpp #includes the .h.

    The program also has a main(), in it's own .cpp. It may or may not have an .h, but it does #include the .h files for the classes it uses.

    So when you compile, you will be explitly compiling all the .cpp files together. Alternately, you can build object files from the class files (.cpp/.h pairs), then link them in when you compile the main() seperately. How that is done depends on the tools your are using. It is nice to use a "make" system of some sort -- IDEs often have that build-in -- but not required.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple source files
    By ericad in forum C Programming
    Replies: 10
    Last Post: 01-24-2010, 09:24 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM