Advertisement

Multiple File Inclusion

Started by February 07, 2002 09:51 PM
3 comments, last by pizza box 22 years, 10 months ago
I understand that you can include self-made header files in projects, but what if you had multiple source files? How would you include files that don''t end in .h to your main source file? Also, what kind of things should go in header files and source files? Thanks a lot.
why would you want to include files that dont end in .h or .c/pp to ur main source file?

a header file is used to put definitions in and stuff that you may want to use in other .cpp files.

a .cpp file is an implementation file... you write actual code in it, in general, you can think of a .cpp as program space, while a .h is compiler space.

      //stdafx.h#ifndef _stdafx_#define _stdafx_#include <iostream>using namespace std; char getKey(); void drawStuff();#endif        //main.cpp#include "stdafx.h" //allow access to stuff in stdafx.hvoid main(){ getKey(); drawStuff();}        //stdafx.cpp#include "stdafx.h" char getKey() {} void drawStuff() {}      


in general, you can use a headerfile to define class interfaces, 'library' functions, defines/macros, and constants/enums.

Edited by - evilcrap on February 8, 2002 1:06:43 AM
Advertisement
What I am asking is if I can write anything that you would normally put in a .cpp file ( excluding main() ), in a .h file. Are there any things that you can't or shouldn't put in a .h file? Thanks

Woops, sorry I missed that last part you wrote. Thanks for the help.

Edited by - Sethius on February 8, 2002 2:37:59 PM
As far as i know, the compiler doesn''t differentiate between .cpp and .h files. You should in general put the program code in the .cpp files and all the classes, functions that will be used in more than one .cpp file, definitions and stuff like that in the .h files.

Hope that helped
The .h files are the interface for you .cpp files, so you include the .h files, and be sure that your make file or Visual C++ project includes the cpp files. The compiler will build each file one at a time, and then the Linker will ensure that it is all there.

This topic is closed to new replies.

Advertisement