Solved

URGENT:  Header Files / Linking Problem

Posted on 2003-10-30
8
279 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
Comment Utility
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
Comment Utility
I added them, it didn' t fix the problem.
0
 
LVL 5

Author Comment

by:jjacksn
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

16 Experts available now in Live!

Get 1:1 Help Now