Solved

URGENT:  Header Files / Linking Problem

Posted on 2003-10-30
8
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GUI: DIalog Stacking and Popping in MS C++ 4 87
best sources to up-to-date in C++? 8 102
White board coding practice 3 93
show out valin of json for debugging in visaul c++ 1 47
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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

740 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