PDA

View Full Version : compilation issues: multiple includes?



sfabel
22nd December 2009, 23:30
Hello all,

I'm having problems compiling my code under Linux using Qt Creator (Qt 4.6).

In particular, I have a header file "m.h" like the following:


#ifndef M_H
#define M_H
#include "data.h"

#include <QtGlobal>
#include <QString>

namespace MYNAMESPACE {
class M : public QObject
{
Q_OBJECT
....
}
#endif


With a corresponding implementation "m.cpp":


#include "m.h"

namespace MYNAMESPACE {
....
}


data.h contains constants and helper functions, and also includes QtGlobal and QString.

Now, when I try to compile this, I get errors like these:

m.o: In function M:
[path to qtglobal.h]/qtglobal.h: 1361: multiple definition of `MYNAMESPACE::M::M()'
[path to qtglobal.h]/qtglobal.h: 1361: first defined here

It then proceeds to complain that every single function in m.cpp is already defined in m.cpp (identical line numbers).

I have no idea whatsoever. Why would it claim that my own function/class is previously defined in qtglobal.h, I am even using my own namespace? And then, why do I get errors that complain that a method defined in one file was perviously defined in the same file? This error repeats through my entire program with all functions that I have written. Is it wrong to include QtGlobal or what am I missing?

I think I also should mention that I get a lot of warnings about my Makefile that look like this:

Makefile:[line number]: warning: ignoring old commands for target 'moc_m.cpp'
Makefile:[line number]: warning: ignoring old commands for target 'm.o'

Any help would be greatly appreciated.

Thanks,
Stephan

nateriver
23rd December 2009, 05:34
In particular, I have a header file "m.h" like the following:


...
namespace MYNAMESPACE {
class M : public QObject
{
Q_OBJECT
....
#closing bracket?
}
#endif



Did you miss closing bracket ('}') in the marked position? Or just missed it when posting?
From my experience with C++, such strange errors usually happen when there is a syntax error somewhere, for example missing bracket, semi-colon or smt like that.



Makefile:[line number]: warning: ignoring old commands for target 'moc_m.cpp'
Makefile:[line number]: warning: ignoring old commands for target 'm.o'


But most likely it seems that moc (qt's meta-object compiler) doesn't run when building for some reason, so C++ compiler tries to compile Qt-specific stuff and fails.

P.S. If data.h includes QtGlobal and QString there is no need to include them in m.h.

totem
23rd December 2009, 08:19
isn't it more like this (keeping .h the same) :



#include "m.h"

using namespace MYNAMESPACE ;

// aso..


because indeed you declare your namespace twice. or am I wrong ?

lonepsycho
23rd December 2009, 09:33
not sure, but maybe semicolon is missing?

nateriver
23rd December 2009, 10:12
isn't it more like this (keeping .h the same) :



#include "m.h"

using namespace MYNAMESPACE ;

// aso..


because indeed you declare your namespace twice. or am I wrong ?

Semantically that's the same, since namespace itself cannot be declared; by putting statements inside namespace block you just tell compiler that they belong to that namespace. Namespaces are just syntactic sugar after all (they are not translated to assembly code, and used only to distinguish methods and classes with the same name during compilation).
After all, you can just add "MYNAMESPACE::" before class name when implementing methods, that'll give the same result too.

My bet is that sfabel's problem lies in the fact that files generated by moc are not being compiled.

Makefile:[line number]: warning: ignoring old commands for target 'moc_m.cpp'
This line tells that make ignored command for 'moc_m.cpp' processing, which is a file generated by moc most probably.

lonepsycho
23rd December 2009, 11:29
i meant semicolon in .h file not cpp one. can you provide .h file contents?