Thread: question about multible source files

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    question about multible source files

    I have two files. One is main.cpp and one is testfunction.cpp. testfunction.cpp has void testfunction()... main.cpp has int main()...

    If I want to use testfunction from main it brings an error that no such function exists. But I am sure that testfunction.cpp is also getting compiled because if I add an error there I will get an compile error.

    The only solution I found was to use main.h, main.cpp + testfunction.h and testfunction.cpp. All includes I need for main.cpp are included in main.h. testfunction.cpp includes first testfunctin.h and testfunction.h is getting included by main.h.

    I think this could be a bad way. Is it?

    Any other way to use testfunction from main.cpp?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That is not a bad way.
    I would say if the functions declared in main h do not depend on anything from testfunction.h then the usual way would be that you remove the #include "testfunction.h" from main.h and include both main.h and testfunction.h into main.cpp.
    One rule that I usually follow is that I never include any headers into another header that are not needed inside that header. I'd rather use forward declarations if that is enough.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If I want to use testfunction from main it brings an error that no such function exists.
    Be careful here, a compile error is different from a link error.
    Linking is when you join all the object files of your program together to form a working program.

    Given the description, I would say

    testfunction.h prototypes the functions in testfunction.cpp, and is included by testfunction.cpp and main.cpp.
    main.h prototypes the functions in main.cpp, and included by main.cpp
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    9

    Wink

    "main.h"??? <-- Obviously something wrong here.

    If I want to use testfunction from main it brings an error that no such function exists. But I am sure that testfunction.cpp is also getting compiled because if I add an error there I will get an compile error.
    Well, if there's something that won't compile, the compiler won't get to anything else after that error.

    Be sure to use this with all your source files:
    Code:
    #ifndef _MYSOURCEFILENAME_H
    #define _MYSOURCEFILENAME_H
    
    /*  Your source file  */
    
    #endif

    *make* files would teach you a good way of keeping track of dependencies...

    Basically:

    wherever main is needs (#include "testfunction.cpp")

    testfunction.cpp needs(#include "testfunction.h")

    testfunction.h needs no other dependencies.

  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
    > wherever main is needs (#include "testfunction.cpp")
    It's a really (really) bad idea to #include source code.

    If two different files needed testfunction, and you included the source in both, then you'd end up with a fist full of multiply declared symbols.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    > wherever main is needs (#include "testfunction.cpp")
    It's a really (really) bad idea to #include source code.

    If two different files needed testfunction, and you included the source in both, then you'd end up with a fist full of multiply declared symbols.
    OK, to make you (Salem) happy, and to compile your (sept) program:

    Your Files:

    -main.h -main.cpp -testfunction.h -testfunction.cpp

    main.cpp includes:

    -testfunction.h -main.h

    testfunction.h includes:

    -testfunction.cpp


    as an aside, there won't be multiple includes with the #ifndef directive
    Last edited by laserlight; 09-23-2007 at 09:56 AM. Reason: Fixed quote tag.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by entropyrexor View Post
    as an aside, there won't be multiple includes with the #ifndef directive
    True no multiple includes. But multiply declared symbols. ( linker errors )
    Kurt

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    True dat ZuK.

    I was only trying to get sept's program to compile with the least bit of confusion, i didn't mean to advocate such sloppiness, sorry guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  3. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. linking source files
    By estranged in forum C Programming
    Replies: 9
    Last Post: 04-03-2002, 06:38 PM