Error del compilador "collect2: Ld devolvió 1 estado de salida" al usar clases con QObject (QT 4.7 con Qt Creator)
Pregunta
Tengo la siguiente situación en mi proyecto actual: "collect2: Id devolvió 1 estado de salida" devuelto por el compilador con el siguiente código:
#ifndef BASE_02_H
#define BASE_02_H
#include <QtCore>
class Base_02
{
public:
Base_02();
virtual void method()=0;
};
#endif // BASE_02_H
#include "base_02.h"
#include <QtCore>
Base_02::Base_02()
{
}
//----------------------------------------------------------------------------------------------------------------------------------
#ifndef DERIVED_02_H
#define DERIVED_02_H
#include <QtCore>
#include "base_02.h"
class Derived_02 : public Base_02
{
public:
Derived_02();
void method();
};
#endif // DERIVED_02_H
#include "derived_02.h"
#include <QtCore>
Derived_02::Derived_02()
{
}
void Derived_02::method()
{
qDebug() << "Derived_02::method()";
}
//----------------------------------------------------------------------------------------------------------------------------------
#ifndef BASE_H
#define BASE_H
#include <QtCore>
class Base : public QObject
{
Q_OBJECT
public:
Base(Base* p=NULL);
virtual void method()=0;
};
#endif // BASE_H
#include "base.h"
#include <QtCore>
Base::Base(Base* p)
{
}
//----------------------------------------------------------------------------------------------------------------------------------
#ifndef DERIVED_H
#define DERIVED_H
#include <QtCore>
#include "base.h"
class Derived : public Base
{
Q_OBJECT
public:
Derived(Derived* p=NULL);
void method();
};
#endif // DERIVED_H
#include "derived.h"
#include "derived_02.h"
#include <QtCore>
Derived::Derived(Derived* p)
{
}
void Derived::method()
{
Derived_02 d;
d.method();
}
//----------------------------------------------------------------------------------------------------------------------------------
#include <QtCore/QCoreApplication>
#include <QtCore>
#include "base.h"
#include "derived.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Derived* derived = new Derived();
return a.exec();
}
El compilador dice lo siguiente:
... mingw32-make: Ingresando directorio
C:/Entwicklung SVN/debug test/dataSenderReceiverExample' C:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug mingw32-make[1]: Entering directory
C: / Entwicklung SVN / debug test / dataSenderReceiverExample ' g ++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I "...... \ Qt \ 2010.04 \ qt \ include \ QtCore" -I " .. \ Qt \ 2010.04 \ qt \ include "-I" .. \ dataSenderReceiver "-I" ...... \ Qt \ 2010.04 \ qt \ include \ ActiveQt "-I" depurar "-I" ... ... \ Qt \ 2010.04 \ qt \ mkspecs \ win32-g ++ "-o debug \ main.o main.cpp main.cpp: En la función 'int main (int, char **)': main.cpp: 14: advertencia: variable no utilizada 'derivada' g ++ -enable-stdcall-fixup -Wl, -enable-auto-import -Wl, -enable-runtime-pseudo-reloc -Wl, -subsystem, console -mthreads -Wl -o debug \ dataSenderReceiverExample.exe debug / main.o -L "c: \ Qt \ 2010.04 \ qt \ lib" -L ../ dataSenderReceiver / debug -ldataSenderReceiver -lQtCored4 mingw32-make [1]: Saliendo del directorioC:/Entwicklung SVN/debug test/dataSenderReceiverExample' mingw32-make: Leaving directory
C: / Entwicklung SVN / debug test / dataSenderReceiverExample ' ../dataSenderReceiver/debug/libdataSenderReceiver.a(derived.o):C:\Entwicklung SVN \ debug test \ dataSenderReceiver / derivado.cpp: 14: referencia no definida aDerived_02::Derived_02()' ../dataSenderReceiver/debug/libdataSenderReceiver.a(derived.o):C:\Entwicklung SVN\debug test\dataSenderReceiver/derived.cpp:15: undefined reference to
Derived_02 :: method () ' collect2: ld devolvió 1 estado de salida mingw32-make [1]: * [debug \ dataSenderReceiverExample.exe] Error 1 mingw32-make: * [debug] Error 2 El Prozess "C: /Qt/2010.04/mingw/bin/mingw32-make.exe" wurde mit dem Rückgabewert% 2 beendet. Fehler beim Erstellen des Projekts dataSenderReceiverExample (Ziel: Desktop) Beim Ausführen des Build-Schritts 'Make'
¡Por favor ayuda!
Solución
#include "base_02.h"
#include <QtCore>
tiene que ser:
#include <QtCore>
#include "base_02.h"
¡Entonces se compila!
Otros consejos
Tuve este problema en la siguiente situación:
Di
A es un proyecto ejecutable (p.i. consola) y B y C son proyectos de biblioteca (f.i. static lib) y A usa B y B usa C.
Entonces
A tiene que conocer la biblioteca B y la biblioteca C a través del archivo .pro y B tiene que conocer la biblioteca C a través del archivo .pro.
Di
en la biblioteca de archivos A .pro, C se indica antes que la biblioteca B:
LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"
LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"
Entonces
se produce exactamente el error descrito anteriormente.
Solución:
Nombre la biblioteca B antes que la biblioteca C en el archivo A .pro:
LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"
LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"
I got same problem here...??
#include<iostream>
using namespace std;
class employee{
protected:
int id;
string name;
int day;
int salary;
public:
virtual void calsalary()=0;
};
class facultyemp:public employee{
public:
static int k;
facultyemp(){
k++;
id=k;
}
void setname(){
cout<<"Enter name:";
cin>>name;
}
void setday(){
cout<<"Enter worked days:";
cin>>day;
}
void setsalary(){
cout<<"Enter salary pr day:";
cin>>salary;
}
void getname(){
cout<<"Name: "<<name;
}
void getid(){
cout<<"\nID: "<<id;
}
void calsalary(){
cout<<"The net salary: ";
cout<<day*salary;
}
};
static int k=0;
int main(){
employee *e;
facultyemp f;
e=&f;
f.setname();
f.setday();
f.setsalary();
f.getname();
f.getid();
e->calsalary();
}