PDA

View Full Version : QOBJECT macro / EXTERN C incompatibility issue



talha
9th January 2014, 23:30
Hi,

I am having a problem with trying to include c headers and c files using extern C in a c++ header file which contains a class that inherits QOBJECT and contains the QOBJECT macro. For example:



#include <qobject.h>
#include <vector>

extern "C"
{
#include "extApi.h"
#include "extApi.c"
#include "extApiPlatform.h"
#include "extApiPlatform.c"
}

class VrepClient: public QObject
{
Q_OBJECT

public:
VrepClient();
~VrepClient(){};
void setJoints(std::vector<float> jntValues);
bool connected() {return clientID != -1 ? true : false;}

public slots:
void simulate();

private:
int port;
const simxChar* address;
int clientID;
std::vector<int> jointHandles;
std::vector<float> jointValues;

bool stopSim;
};


The program does not compile and generates linkage errors for all the methods in the .c files. Some examples:



1>vrepClient.obj : error LNK2005: _simxGetObjectChild already defined in moc_vrepClient.obj
1>vrepClient.obj : error LNK2005: _simxTransferFile already defined in moc_vrepClient.obj
1>vrepClient.obj : error LNK2005: _simxEraseFile already defined in moc_vrepClient.obj
1>vrepClient.obj : error LNK2005: _simxLoadModel already defined in moc_vrepClient.obj


My guess is that during generation of the moc file the c files are included first and then again for the .cpp file generating the error. Moving the extern C bracket to the .cpp solves the problem but then I can't use the c code in the .h file. Any help is appreciated.

Should add I'm using win 7 x64, qt 4.8, vs2010 with qt plugin.

Thanks,

Talha

anda_skoa
10th January 2014, 11:29
Don't include the C files, only the headers.

Source files are usually not guarded against multiple inclusing.

Cheers,
_

talha
10th January 2014, 16:46
Only removing the .c files causes unresolved errors so I've overcome the issue (I think) by converting the external c code into a .lib which I then include in my program.

anda_skoa
10th January 2014, 17:22
Obviously the C code still needs to be compiled.
That is what the SOURCES variable in your .pro file is for.

Or, as you seem to have figured out yourself, putting them into a library.

Cheers,
_