Solved

URGENT:  Header Files / Linking Problem

Posted on 2003-10-30
8
280 Views
Last Modified: 2012-05-04
I have a COM dll I am writing with 4 different interfaces / classes.  I have some classes, structs and globals defined in a .H file (its all in the .H file, there is no cpp file)

When I include the .H file in one of the headers for the 4 classes, I have no problem.

When I include the .H file in two of the headers for the 4 classes, I get all sorts of

SYMBOLICFucntionName already definien in Object2.obj, where Object2 is the second object I added the #include directive to.  There are no errors for Object1, the original object that #included my header with objects and enums.  

Am I not allowed to do this?  If not, how do I restrucutre the linking/files so that I can do something like this?  
If so, why is this not working?
0
Comment
Question by:jjacksn
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 9652050
Do you have "multiple inclusion guards", e.g.

//MyHeader.h:
#ifndef __MyHeader_h
#define __MyHeader_h

// rest of the header file goes here...

#endif
?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9652102
I added them, it didn' t fix the problem.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9652120
Also, when I don't have the header in there, I can't access the objects in the class that is causing the problems, so I don't think I have multiple inclusion problems.  I think it has something to do with the fact that they are all in the COM class perhaps? the .h file is creating global objects.  just a thought.  
0
 
LVL 86

Expert Comment

by:jkr
ID: 9652157
This _usually_ fixes the problem you described. Are you sure the #ifndef/#endif part encloses *all* of your file? Could you post the code?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9652320
>its all in the .H file, there is no cpp file
You have to split them on cpp and h.
There is no good way to solve the problem, if you have a code in your H file not only headers.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9652350
Other way:

---myh.h---
void SomeFunc(void);

#ifndef CODE_ALREADY_DEFINED
void SomeFunc(void)
{
}
#endif
--------------

---file1.cpp---
#include "myh.h"
----------------

---file2.cpp---
#define CODE_ALREADY_DEFINED
#include "myh.h"
----------------

---file3.cpp---
#define CODE_ALREADY_DEFINED
#include "myh.h"
----------------

...
0
 
LVL 6

Assisted Solution

by:GloomyFriar
GloomyFriar earned 150 total points
ID: 9652375
One correction:

---myh.h---
#ifndef ___MYH_H_
#define ___MYH_H_

/* Put all declarations here */
void SomeFunc(void);

#ifndef CODE_ALREADY_DEFINED
/* Put all code in the such sections */
void SomeFunc(void)
{
}
#endif

#endif
--------------
0
 
LVL 9

Accepted Solution

by:
_ys_ earned 250 total points
ID: 9653221
>> its all in the .H file, there is no cpp file
This will lead to multiple definitions of the objects within, guaranteed - unless of course you only have a single compilation unit (cpp file), which would be unusual.

Create a .cpp file, and move _all_ of the definitions to there. Your .h file, if neessary, can extern all those items removed.

GloomyFriar gave a good example, a function. If your current .h file has this

void SomeFunc(void)
{
}

then that would be moved to the .cpp, and your .h file would retain

extern void SomeFunc(void);

If you post the contents of your .h file, I'm sure somebody could aid in splitting them up with you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 127
C++ question 3 62
computer science syllabus 3 70
Least Squares Curve Fitting 4 61
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now