Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Pls hpw does one ensure that only one copy of a file ia # included in the complilation when several files and base class file require thesame file in an mfc application.
I have been battling this all day.The compliler keep complaining of a redefinition.
Posted
Comments
[no name] 17-May-14 8:40am    
http://en.wikipedia.org/wiki/Pragma_once

In the .H file put protection top and bottom like this

// PROTECTION TO STOP MULTIPLE UNIT LOADING
#ifndef _MYUNIT_									
#define _MYUNIT_

// YOUR NORMAL CURRENT .H DATA HERE

#endif


I always use _UNITNAME_ for each and every unit as a standard and that is the almost standard way to do it. If you had of thought hard you would have simply gone at looked at the ones in the include directory on your compiler system which will usually do the same thing :-)

This is the top of strings.h in Visual Studio 12 & 13
/***
*string.h - declarations for string manipulation functions
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       This file contains the function declarations for the string
*       manipulation functions.
*       [ANSI/System V]
*
*       [Public]
*
****/

#pragma once

#ifndef _INC_STRING
#define _INC_STRING
 
Share this answer
 
v3
Use header guard in the header file:
C++
#ifndef _HEADER_NAME_
#define _HEADER_NAME_
...
...
...
#endif

When the first time this header is loaded, _HEADER_NAME_ is defined. So that prevents redefinition.
 
Share this answer
 
Comments
Richard MacCutchan 27-May-14 4:18am    
This method is deprecated now, see http://msdn.microsoft.com/en-us/library/4141z1cx.aspx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900