Thread: Splitting up a C++ Program?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    11

    Splitting up a C++ Program?

    Please forgive me if this topic is redundant. I've searched and couldnt find anything.

    I'm a bit confused here as i'm trying to split my cpp program into 3 separate files using Visual Studio.

    main.cpp includes interface.h
    interface.h include implementation.cpp
    implementation.cpp includes interface.h

    I get an error message too many include files : depth = 1024 1> Generating Code...

    on #include "interface.h" within implementation.cpp.

    Please tell me what i'm doing wrong?

    I also went into the solution explorer and tried manually adding the .h file under "headers" and .cpp files under" source files". nothing is working.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is obvious. Look carefully:

    main.cpp includes interface.h
    interface.h include implementation.cpp
    implementation.cpp includes interface.h

    So interface.h includes implementation.cpp, and implementation.cpp includes interface.h, and interface.h includes implementation.cpp, and implementation.cpp includes interface.h... wait, haven't we already seen this before?
    Yes we have! They include each other!

    Anyhow, you should never include .cpp files. Add them to your project only. .h files you should include.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    11
    Thanks!
    If I exclude the line, and the CPP to my project as a "source file"
    all of the functions inside of implementation.cpp are not found.
    Im probably doing something wrong there. What would be the proper way to include the implementation.cpp file in Visual Studio?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should make function declarations aka prototypes of all your functions, and put those in a header file. Then you include the header file in the files that needs to call those functions.
    It will work beautifully you'll see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There's a FAQ entry on multi-file programs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM