3
moc_mm.o: In function `Counter::metaObject() const':
moc_mm.cpp:(.text+0x0): multiple definition of `Counter::metaObject() const'
moc_joystick.o:moc_joystick.cpp:(.text+0x0): first defined here
moc_mm.o:(.rodata+0x0): multiple definition of `Counter::staticMetaObject'

Which part of the code am I supposed to present here, w.r.t this error?
What is meaning of this error?

I have done make clean, but that doesn't help. The function name metaObject seems to be internal to Qt.

EDIT 1:
Adding the header file:

#include <stdio.h>
#include <QObject>
#ifndef __JOYSTICK_H__
#define __JOYSTICK_H__

#define JOYSTICK_DEVNAME "/dev/input/js0"

#define JS_EVENT_BUTTON         0x01    /* button pressed/released */
#define JS_EVENT_AXIS           0x02    /* joystick moved */
#define JS_EVENT_INIT           0x80    /* initial state of device */


struct js_event {
    unsigned int time;  /* event timestamp in milliseconds */
    short value;   /* value */
    unsigned char type;     /* event type */
    unsigned char number;   /* axis/button number */
};

struct wwvi_js_event {
    int button[11];
    int stick_x;
    int stick_y;
};

class Counter : public QObject
 {
    Q_OBJECT
    private:
        int m_value;

    public:
        Counter() { m_value = 0; }

        int value() const { return m_value; }

    //public slots:
        int open_joystick();
        int read_joystick_event(struct js_event *jse);
        //void set_joystick_y_axis(int axis);
        //void set_joystick_x_axis(int axis);
        void close_joystick();
        int get_joystick_status(struct wwvi_js_event *wjse);

    //signals:
        //void valueChanged(int newValue);
 };
#endif

Output:

anisha@linux-dopx:~/Desktop/anishaJoystick> qmake -project
anisha@linux-dopx:~/Desktop/anishaJoystick> qmake
anisha@linux-dopx:~/Desktop/anishaJoystick> make
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o anishaJoystick joy.o mm.o moc_joystick.o moc_mm.o    -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 
moc_mm.o: In function `Counter::metaObject() const':
moc_mm.cpp:(.text+0x0): multiple definition of `Counter::metaObject() const'
moc_joystick.o:moc_joystick.cpp:(.text+0x0): first defined here
moc_mm.o:(.rodata+0x0): multiple definition of `Counter::staticMetaObject'
moc_joystick.o:(.rodata+0x0): first defined here
moc_mm.o: In function `Counter::qt_metacast(char const*)':
moc_mm.cpp:(.text+0x20): multiple definition of `Counter::qt_metacast(char const*)'
moc_joystick.o:moc_joystick.cpp:(.text+0x30): first defined here
moc_mm.o: In function `Counter::valueChanged(int)':
moc_mm.cpp:(.text+0x70): multiple definition of `Counter::valueChanged(int)'
mm.o:mm.cpp:(.text+0x10): first defined here
moc_mm.o: In function `Counter::qt_metacall(QMetaObject::Call, int, void**)':
moc_mm.cpp:(.text+0xb0): multiple definition of `Counter::qt_metacall(QMetaObject::Call, int, void**)'
moc_joystick.o:moc_joystick.cpp:(.text+0x20): first defined here
collect2: ld returned 1 exit status
make: *** [anishaJoystick] Error 1

Source file:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "joystick.h"

static int joystick_fd = -1;

int Counter::open_joystick()
{
    joystick_fd = open(JOYSTICK_DEVNAME, O_RDONLY | O_NONBLOCK); // read write for force feedback?
    if (joystick_fd < 0)
        return joystick_fd;

    // maybe ioctls to interrogate features here? 

    return joystick_fd;
}

int Counter::read_joystick_event(struct js_event *jse)
{
    int bytes;

    bytes = read(joystick_fd, jse, sizeof(*jse)); 

    if (bytes == -1)
        return 0;

    if (bytes == sizeof(*jse))
        return 1;

    printf("Unexpected bytes from joystick:%d\n", bytes);

    return -1;
}

void Counter:: close_joystick()
{
    close(joystick_fd);
}

int Counter::get_joystick_status(struct wwvi_js_event *wjse)
{
    int rc;
    struct js_event jse;
    if (joystick_fd < 0)
        return -1;

    // memset(wjse, 0, sizeof(*wjse));
    while ((rc = read_joystick_event(&jse) == 1)) {
        jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
        if (jse.type == JS_EVENT_AXIS) {
            switch (jse.number) {
            case 0: wjse->stick_x = jse.value;
                break;
            case 1: wjse->stick_y = jse.value;
                break;
            default:
                break;
            }
        } else if (jse.type == JS_EVENT_BUTTON) {
            if (jse.number < 10) {
                switch (jse.value) {
                case 0:
                case 1: wjse->button[jse.number] = jse.value;
                    break;
                default:
                    break;
                }
            }
        }
    }
    // printf("%d\n", wjse->stick1_y);
    return 0;
}

//#if 0
/* a little test program */
int main(int argc, char *argv[])
{
    int fd, rc;
    int done = 0;

    struct js_event jse;

    Counter c;
    fd = c.open_joystick();
    if (fd < 0) {
        printf("open failed.\n");
        exit(1);
    }

    while (!done) {
        rc = c.read_joystick_event(&jse);
        usleep(1000);
        if (rc == 1) {
            printf("Event: time %8u, value %8hd, type: %3u, axis/button: %u\n", jse.time, jse.value, jse.type, jse.number);
        }
    }
}
//#endif
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 2
    Try running `qmake` again on the project *before* another clean/build. In Qt-world, sometimes `make clean` isn't enough, particularly when using signals and slots, which is what is sounds like is happening. – Dave Mateer Nov 17 '11 at 13:19
  • You have the error message `moc_mm.o: In function 'Counter::valueChanged(int)'` and `Counter` appears to have `void valueChanged(int newValue);` commented out. Could this be your problem? – sam-w Nov 18 '11 at 08:43
  • @sjwarner I have added the source file. :( – Aquarius_Girl Nov 18 '11 at 08:54

2 Answers2

2

This error also happens when you accidentally use the Q_OBJECT macro twice for the same class.

For example, I just had this during a messed up transition from a source code layout with declaration and implementation in the .cpp file (as described here) to the typical layout of splitting these into .h and .cpp. Mapped to your example code, my code was as follows:

  • joystick.h: Exactly as in the question, including this key section with the macro call:

    class Counter : public QObject {
        Q_OBJECT
    
        // ... method declarations here ...
     };
    
  • joystick.cpp, including the accidental second macro call from leftovers of the original implementation that was completely here:

    class Counter : public QObject {
        // The erroneous SECOND call of the macro.
        Q_OBJECT
    
        // ... method definitions here ...
    };
    
    // The obligatory include when using the Q_OBJECT macro in a .cpp file.
    #include "joystick.moc"
    
tanius
  • 14,003
  • 3
  • 51
  • 63
2

You probably have multiple moc_* files, perhaps old ones after some renames, in your directory, and running qmake -project has included the old ones in your build. It finds multiple moc_* declarations for the same names (moc_mm.cpp and moc_joystick.cpp) and conflicts.

Remove all moc_* files by hand and re-create the .pro file with -project.

Tatu Lahtela
  • 4,514
  • 30
  • 29