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.
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?
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.
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.
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.
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>
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>
Bookmarks