Compile a "mutiple-source-files" program

I need to compile and link a program with a header file (.h) and two source code files (.cpp). ( I need to do this from scratch. The .cpp files have not yet been created, and I don't know how to create the .h file.) The .h file (header file) defines the Class's interface with Function Prototypes. The next file defines the member functions in a seperate source code file. The last source code file contains function main and executes the program.

The compiler that I am using is Microsoft's Visual C++ Express 2005. Could someone help me with the steps to compile and link these three pieces of code. I am very new at this.
OK, in MS Visual C++ 2005 what you need to do is as follows
1) From the menu; File, New, Project. This will bring up the New Project dialog, select Win32 from project types, Win32 Console Application from Templates, add a Name and Location for your project in the relevant boxes and click OK. Choose next on teh Win32 Application Wizard, then check the 'Empty Project' box adn click Finish.
2) You now need the 'Solution Explorer' window - this is a treeview of all the files in the project (View, Solution Explorer from menu). Under your project naem you shoudl see three folders; Header files, Resource Files and Source Files.
3) Right click on Header files, select Add, New Item to bring up the Add New Item Dialog. Select Code as the Category, Header File(.h) as the template. Add a name for your header file & click Add.
4) Repeat in Source Files folder for the two C++ Files (.cpp). Give the source file for the member function the same name as the header file.
That get you the three files, once you have the relevant code in each use Build, Build Solution (F7) from the menu to compile.
You will need a #include "headerfilename.h" line in both the .cpp files to include the definitions in the source - the linker will automatically link the two cpp files.

I've assumed you are creating a basic console app in the above.
If you choose to run it from debug (Debug, Start Debugging) in the IDE then you will need some way of stopping the app before it compleats if you want to see the output.
The simplest is to add System("pause") just before the end of main - just remember this is a quick hack to make life easy when learning or writing short test apps, and NEVER use it in code that will go any further than that!
Excellent. So good, so far. One more question though. I am confused on how to get from the end of "Step 3" to "Step 4." Specifically, after typing in the code for the .h file, what do I do to save it before going on to create the other two .cpp files. On my other projects that contained only one .cpp file, I would go right to build after typing in the code. I know I don't do that with the header file. From the limited documentation that I have, I believe I need to go to the Debug menu. Is that right?

I really appreciate your help on this.
No, in fact you can (and I often do) create the blank files first, then go and enter the code into each.
I would personally do sonething like
1) Create the 3 blank files
2) Add the #include to the .cpp files
3) declare main()
4) enter the outline of my header (with just a constructor) in the .h
5) define the constructor in the .cpp
6) declare a dummy instance of the class in main
7) Build to check the 3 files link OK.
8) Add more the the .h
9) Add more to the class derfinition in the .cpp
10) Build again to check all OK
11) Repeat 8-10 until the class was done :-)
Wow. I will try to do that next time. This time, I am copying the example straight from a book. I am still comfused on how to save the .h file (even if it was blank), and go on to the next step of creating the other two files. Can I just right click on the Source Files folder on the tree and assume (hope) that the .h file will be saved?
OK, thats what I did. Clicked on the Source File folder on the tree. Went to New and hit Add. The .h code was still there when I double clicked it. Just out of curiosity, what is the folder "Resource Files" on the Solution Tree for?

Thanks again Faldrax
It's for things you want included that are not headers or source files - Icons, graphics, sound files, etc.
It gets used more when you are on a Windows forms type application than Console Application.

Glad to be able to help - I remeber well the feeling of 'What! How do I get started!!!" when faced with an IDE the size of Visual Studio.
Topic archived. No new replies allowed.