CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2010
    Posts
    9

    Working with multiple cpp and h files

    Hi there,

    I am currently working on a project where I have to create a program that adds, removes, lists and searches for contacts and contact information stored in a datafile (.dat). I am going to use a few .cpp files like contadd.cpp, contrm.cpp etc but I was just wondering if I have common functions that I want to run in each of these cpp files, should I create a source file called contlib.cpp (with all the function definitions) and a header file contlib.h to put all the function declarations? Do each of the source files need their own header file? And how do I link the functions cpp file to the other source files so that they can see them?

    Does this make sense? I can clarify some things if you're confused.

    Thanks heaps,
    Aonghas

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Working with multiple cpp and h files

    Yes, it makes sense. The general idea is that source code in C/C++ is split in 2 files: headers (usually) contain declarations of types, functions, constants, etc. CPP files contain the implementation of those types, functions, etc. All you have to do to make available in a file B.cpp the symbols (functions, types, etc.) defined and implemented in the pair A.h/A.cpp, is to include the header A.h. Of course this is true if you use a development environment that supports projects and you just add files to the project (and set building options). If you want to manually build the sources from command line, than there's another story. So what do you use for creating your application?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Aug 2010
    Posts
    9

    Re: Working with multiple cpp and h files

    Hi cilu,

    Thanks for your reply. That makes sense to me I think. So if I have all my common functions declared in functions.h and defined and implemented in functions.cpp, can I just use #include "functions.h" in my program.cpp file? Will the functions.h file know to look in the functions.cpp file automatically to find the functions because they have the same name? I'm just using gedit to write up the source files.

    Thanks
    Aonghas

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Working with multiple cpp and h files

    Quote Originally Posted by aonghas View Post
    Will the functions.h file know to look in the functions.cpp file automatically to find the functions because they have the same name?
    No. You'll need to tell the compiler to build functions.cpp in addition to any other source files you have, and then link it into the executable. Usually, the linking is implied by the fact that you're compiling them together. For most IDEs, adding both source files to the project is enough.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Working with multiple cpp and h files

    No, when you compile, you compile functions.cpp, it will automatically be able to find the declarations because of the #include, not the other way around.

    You're new to C++, so I wouldn't recommend gedit. gedit is fine, but requires you to use the command line or a makefile to compile. IDEs do all of the work of managing files and generating the proper compile commands for you. I recommend installing Code::Blocks. It should be in the synopsis manager, if not, you can find it on google, it's very popular.

  6. #6
    Join Date
    Aug 2010
    Posts
    9

    Re: Working with multiple cpp and h files

    Oh yeah I mean I'd compile the program.cpp file right? That includes the int main() function. The functions.cpp file simply has all the function definitions. So then in the program.cpp should I just #include both functions.h and function.cpp? I could effectively just put all my functions at the top of my program.cpp file but I just want to have a common functions.cpp file so other source files can use them too...

    In my course I have been told to use gedit and compile in the terminal using g++ <source.cpp> -o <output>

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Working with multiple cpp and h files

    Quote Originally Posted by aonghas View Post
    should I just #include both functions.h and function.cpp?
    No. Never, ever, ever #include a .cpp file. (If you're using templates, it may be prudent to #include an implementation file, but don't call it .cpp. Don't worry about that yet though.)

    In my course I have been told to use gedit and compile in the terminal using g++ <source.cpp> -o <output>
    Well then,
    g++ program.cpp functions.cpp -o <output>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured