Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

URGENT:  Header Files / Linking Problem

Posted on 2003-10-30
8
Medium Priority
?
289 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

876 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