Question:
#ifndef in C++ not working (multiple definitions)?
ZTat
2008-07-04 07:18:50 UTC
Hi. I am new to C++, and I'm trying to create a simple program that will call a few functions from an external dll. It will have a class that will interface the DLL. The main will use this class to run dll commands. The dll definitions for calls are in library.h

Here are the details of the files in my project:

obj.cpp:
#include "obj.h"

obj::obj(){
}

obj::~obj(){
}


obj.h:
#ifndef OBJ_H
#define OBJ_H
#include "library.h"

class obj{
...
};
#endif

main.cpp:
#include "myobj/obj.h"
int main(void){
obj myObject;
myObject.DoSomething();
}

library.h:
#ifndef LIB_H
#define LIB_H
...
#endif


The errors I get are 'multiple definition' errors.
It looks like when compiling/linking the preprocessor includes "library.h" twice, although I am using a #ifndef directive before. I've read that C++ compiles each object main.cpp and obj.cpp without passing #define between the two and thats why the #ifndef doesn't work.

Any help will be appreciated.
Four answers:
justme
2008-07-04 08:42:58 UTC
Yes, I have seen that problem too. What I do to get around it is have one main file (the one that gets compiled first) that I put my includes in, so they wont get included multiple times, from multiple files. Hope this helps, because its a tricky problem that I wish they would fix.
Jason
2008-07-04 13:03:55 UTC
Yes, library.h will be included when compiling obj.cpp, and it will be included when compiling main.cpp.



This should be fine.



However, it probably has something two do with one of these two issues:



1) library.h has some instance definitions of some variables or functions, or



2) precompiled headers are playing up. To check against this, turn off the precompiled headers option if you have it, and clean the project



For 1) what might be the case is that you have an instance of a variable in library.h, e.g.



int i;



or



SomeClass c;



or an instance of a function declared such as



int f()

{

// body

}



What you need instead is an extern definitions such as



extern int i;



extern SomeClass c;



int f();



or else to inline the function:



inline int f()

{

// body

}



Without seeing the precise error, it is difficult to say what is occurring, but look at the error, it will probably tell you what variable is the problem.
2016-04-07 12:45:54 UTC
Taz is correct. You must include Rectangle.h in Rectangle.cpp. Putting the constructor in the .cpp file is one choice. Another is to put it in the header file as an inline, like this: class Rectangle // Rectangle class definition { private: int width; public: Rectangle() { width=0; } void getWidth(); }; Or: class Rectangle // Rectangle class definition { private: int width; public: Rectangle() : width(0) { ; } void getWidth(); }; If a function is very simple and unlikely to ever change, it makes sense to put it in the header file. If it is complex or likely enough to change that its implementation shouldn't be considered part of the class interface, put it in the .cpp file.
bob j
2008-07-04 07:43:58 UTC
Looks right to me maybe if you post the entire error.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...