multiple definition of a function
I want to extend an open source project. Inside the project there is a header file named inet.h which has the following structure:
#ifndef __IPV4UNDERLAYCONFIGURATOR_H__
#define __IPV4UNDERLAYCONFIGURATOR_H__
class foo{
method1();
}
bool method3(){
//method implementation
};
#endif
I have created a header file name extinet.h within which I include intet.h and it looks like this
[...]
include "inet.h"
class foo2:public foo{};
[...]
When I try to compile the project I get a multiple definition error for method3. Is there a way to avoid this error without modifying inet.h (e.g, without declaring method3 to be inline)?
Thanks
Re: multiple definition of a function
Do not define the method 'method3' in header file.
I also see syntax error issues.
Re: multiple definition of a function
Code:
class foo
{
method1();
}
bool method3()
{
//method implementation
};
One of the reasons I don't like that style of using braces. When you line up the braces it's easier to see what may be going on. I guess that's not your real code though, which makes it hard to answer your question.
Re: multiple definition of a function
This not the real code. The problem is with the function redefinition and not any syntax problem. The function method3 is declared and defined in inet.h and !!I can not!! modify it. Every time I include inet.h the redefinition error occurs during the linkage phase. Again I can not modify inet.h
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
Every time I include inet.h the redefinition error occurs during the linkage phase. Again I can not modify inet.h
Why not? Who created inet.h? Whoever did create that file, did a lousy job of it.
You have a few options.
1) Change inet.h, or
2) just include inet.h (and use it) in only one module, or
3) If you must use it in another module, you have to enter the function declaration within the CPP file "by hand", and not do it using the include file:
Code:
// Some file.cpp
bool method3();
void func()
{
method3();
}
Assume the above is the entire file. You declare the method3() function by placing its prototype directly in the source file. Now there will be no linker errors, since the actual definition of the function is still defined in one place.
Regards,
Paul McKenzie
Re: multiple definition of a function
Quote:
Originally Posted by
Paul McKenzie
Why not? Who created inet.h? Whoever did create that file,
People for www.oversim.org
Quote:
1) Change inet.h, or
2) just include inet.h (and use it) in only one module, or
3) If you must use it in another module, you have to enter the function declaration within the CPP file "by hand", and not do it using the include file:
Sorry it did not work. Here is the example code
Code:
//inet.h
#ifndef _INET_H
#define _INET_H
class foo{
void method1();
};
int method3(int x){
return x+2;
}
#endif /* _INET_H */
//exinet.h
#ifndef _EXINET_H
#define _EXINET_H
#include "inet.h"
class foo2:public foo{
};
#endif /* _EXINET_H */
//foo.cpp
#include "inet.h"
void foo::method1(){
}
//foo2.cpp
#include "exinet.h"
I get the followin error: inet.h:14: multiple definition of `method3(int)'
Even if I put the method declaration inside foo2.cpp !!without modifying!!! inet.h I get the same error. Is there any way to do it without modyfing inet.h
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
Is there any way to do it without modyfing inet.h
Yes, but not a good way.
What you can do is write a single source file which includes inet.h. Nothing else should include it. Instead, you make a copy of it, say "myinet.h", and you modify that to simply declare method3 rather than defining it, and likewise any other difficulties.
That way the original header remains unchanged and the method3 implementation only ends up in one translation unit, but you still have all the declarations you need to use elsewhere.
Re: multiple definition of a function
That's impossible because the project already has source files that include inet.h and again I cannot modify these files.
Re: multiple definition of a function
Well, then I don't know of any solution to your problem. The project is broken.
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
You didn't follow what I stated, or let me clarify:
You include inet.h in only one CPP module. You are still including inet.h in multiple modules. Then in the other modules, you directly declare the functions.
Try this simple test -- just include inet.h in only one module, and take it out of the other modules. Now how would you fix the compiler errors for the other modules? Look at the compiler error and understand what it is telling you. All you need to do is declare the missing functions that you're calling.
Regards,
Paul McKenzie
Re: multiple definition of a function
inet.h is already included in multiple modules in the program, and I am supposed to not modify the modules.
The only thing I can make to the code is to add more files (no matter if they are header or source files) but I can not modify existing ones.
So I guess this is not possible?
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
Well, here is example code that proves to you what I stated does work.
MyHeader.h
Code:
#ifndef MYHEADER_H
#define MYHEADER_H
int func1();
int func1()
{
return 0;
}
#endif
Module1.cpp
Code:
#include <iostream>
int func1(); // function is declared
void foo()
{
std::cout << func1();
}
main.cpp
Code:
#include "MyHeader.h"
void foo();
int main()
{
func1(); // call the function to show main() can call it.
foo(); // call the foo function
}
You have two modules. The main.cpp is the only one that actually includes the myheader.h file. The module1.cpp just declares the function and does not directly include it.
Basically all a header file gives you is a convenient way to declare functions and define constants. It is just that for some odd reason, the people you got that inet.h header from has a function body in the header. So the way out of this is simple -- just don't include that header and declare the functions yourself (given that you know the correct declaration).
Regards,
Paul McKenzie
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
inet.h is already included in multiple modules in the program, and I am supposed to not modify the modules.
You aren't allowed to modify the source code? Then that doesn't make any sense. Get the persons who put the source code together and have them fix it, because it's broken.
I am assuming you have a header, and you have modules that you've written, and you're including inet.h in your modules. If you have existing modules written by someone else, drag them in by their ears and have them fix their code so that it does compile and link.
Regards,
Paul McKenzie
Re: multiple definition of a function
The reason I want to include the inet.h in the exinet.h is because I want foo2 class to inherit foo class
Code:
//inside exinet.h
class foo2:public foo{
};
foo class is declared in inet.h file. Is there any way to achieve inheritage without including the inet.h file? For instance the following code does not work
Code:
//exintet.h without include "inet.h"
class foo;
class foo2:public foo{
};
Re: multiple definition of a function
Quote:
Originally Posted by
nikosft
The reason I want to include the inet.h in the exinet.h is because I want foo2 class to inherit foo class
You don't need a header file to inherit. How does "inheriting" any different than "declaring" in my sample code above? Where in C++ does it say "a header file is required..."? You can just take code straight out of any file and just copy it into your source module.
Once more, all a header file does for you is ease of use when declaring functions, classes, constants, so that you don't waste time doing it over and over again for each module, and that you won't make a mistake. That's all, nothing else. Nothing stops you from writing those declarations, constants, classes, etc. "by hand" in your own file.
Again, just copy that entire class defintion out of that header, and place it in the source code yourself.
The bottom line is this -- you must take the inet.h file, and use parts out of it by copying the lines from that file directly into your source code. If you say you can't include inet.h, well, don't include it.
Regards,
Paul McKenzie