Advertisement

Multiple Includes

Started by March 26, 2001 04:34 PM
6 comments, last by Taphreek 23 years, 10 months ago
I have a problem with my project. In one .cpp file I have my main program and the main loop and everything. In other .cpp files I have definitions for various .h files. However, I need to include some header files in multiple .cpp and .h file. An example of this would be printing routines. When I #include them in multiple .cpp files, VC++6 complains saying multiply symbols defined and the such. Anybody have a solution? -Taphreek
-Taphreek
If you use VC++, it will insert the following code for you at the top of each header file

#if !defined(AFX_HEADERFILE_H__F75_09A9_11D5_B8B9_00B0D0FFA9__INCLUDED_)
#define AFX_HEADERFILE_H__F75_09A9_11D5_B8B9_00B0D0FFA9__INCLUDED_

then it includes the contents of the header and then
#end if

This basically checks if the variable is defined, and if it isn''t (the first include), it defines it and then puts in the rest of the file. On subsequent includes, the variable IS defined so it skips the if block and doesn''t do multiple declarations.

VC puts that garbage after the header file name and generates a variable for you. If you want to do this yourself, just make sure you use unique variables, not only within your project, but that would also not conflict with others who may use your code. Maybe you could use the data and time when you write it so its somewhat unique.

Hope this helps.

Simfan.
You sound reasonable. Time to up my medication.

quote:
Original post by Taphreek

I have a problem with my project. In one .cpp file I have my main program and the main loop and everything. In other .cpp files I have definitions for various .h files. However, I need to include some header files in multiple .cpp and .h file. An example of this would be printing routines. When I #include them in multiple .cpp files, VC++6 complains saying multiply symbols defined and the such. Anybody have a solution?

-Taphreek


Venturer
Advertisement
Thanks a lot for your post. So I can''t use a blank project anymore right? I have to do the precompiled header.
-Taphreek
I''m not sure what you mean by blank project. And no you don''t need to have precompiled headers for this to work.

If you post more info, I can try to answer about the blank project.

quote:
Original post by Taphreek

Thanks a lot for your post. So I can''t use a blank project anymore right? I have to do the precompiled header.


Venturer
In all my .cpp files I included "stdafx.h". In my stdafx.h file I put all the headers that I need right where it said to put them. When I try to compile it still gives me multiple symbols defined. But nothing is defined. I declare it, then I do use it is different files. The new project is instead of a skeletal Win32 App I did a blank one.

-Taphreek
-Taphreek
In all of your header files, do something like this:

#ifndef _name_h_
#define _name_h_

/* your code here */

#endif

Where "name" is the name of the header file (common practice). What this effectively does is tell the preprocessor that if this file has already been read, then don't read it again since everything has already been defined.

You can and should do this in C and C++.

Thanks,
Kaewan

Edited by - Kaewan on March 27, 2001 9:41:09 PM
Advertisement
If you are using VC++, include the line

#pragma once

as the first line of each .h and I guess that should solve your problem.
This directive effectively tells the compiler to include the header file only once.

An alternate way of doing that is the method mentioned by simfan but you dont need the junk after the header file.

For e.g.

If you have a header file as Engine.h,

include the following lines as the first lines of Engine.h,

#ifndef __ENGINE_H
#define __ENGINE_H

and include the following line as the last line of Engine.h

#endif __ENGINE_H

This makes the compiler do the following,

If there is no symbol __ENGINE_H defined, then define the symbol __ENGINE_H and do everything in the header file. Now __ENGINE_H is defined and so when the compiler comes accross Engine.h being included in another .cpp, the #ifndef __ENGINE_H fails and hence skips the entire header till #endif __ENGINE_H effectively ignoring the header file from the second time onwards. This makes the header file to be included only once.

For this matter, it is not necessary that if the header file name is Engine.h, the #define should always have __ENGINE_H. All we need is a unique literal for each header file. i.e no other header file in your project should use __ENGINE_H.

Arun

If you''re getting multiple defines errors, try using externs instead of including the same file over and over.

There are certain things you can and cannot include in a file that''s included in more than one file within the project.

Ben
http://therabbithole.redback.inficad.com

This topic is closed to new replies.

Advertisement