Results 1 to 4 of 4

Thread: QOBJECT macro / EXTERN C incompatibility issue

  1. #1
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QOBJECT macro / EXTERN C incompatibility issue

    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:

    Qt Code:
    1. #include <qobject.h>
    2. #include <vector>
    3.  
    4. extern "C"
    5. {
    6. #include "extApi.h"
    7. #include "extApi.c"
    8. #include "extApiPlatform.h"
    9. #include "extApiPlatform.c"
    10. }
    11.  
    12. class VrepClient: public QObject
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. VrepClient();
    18. ~VrepClient(){};
    19. void setJoints(std::vector<float> jntValues);
    20. bool connected() {return clientID != -1 ? true : false;}
    21.  
    22. public slots:
    23. void simulate();
    24.  
    25. private:
    26. int port;
    27. const simxChar* address;
    28. int clientID;
    29. std::vector<int> jointHandles;
    30. std::vector<float> jointValues;
    31.  
    32. bool stopSim;
    33. };
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. 1>vrepClient.obj : error LNK2005: _simxGetObjectChild already defined in moc_vrepClient.obj
    2. 1>vrepClient.obj : error LNK2005: _simxTransferFile already defined in moc_vrepClient.obj
    3. 1>vrepClient.obj : error LNK2005: _simxEraseFile already defined in moc_vrepClient.obj
    4. 1>vrepClient.obj : error LNK2005: _simxLoadModel already defined in moc_vrepClient.obj
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOBJECT macro / EXTERN C incompatibility issue

    Don't include the C files, only the headers.

    Source files are usually not guarded against multiple inclusing.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QOBJECT macro / EXTERN C incompatibility issue

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOBJECT macro / EXTERN C incompatibility issue

    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    talha (10th January 2014)

Similar Threads

  1. Issue with List of objects made of QObject
    By alizadeh91 in forum Qt Programming
    Replies: 4
    Last Post: 3rd June 2013, 21:38
  2. Issue with QObject::connect
    By fish_sticks in forum Qt Programming
    Replies: 10
    Last Post: 3rd March 2012, 10:24
  3. Dreaded Diamond Issue with QGraphicsWidget and QObject
    By perden in forum Qt Programming
    Replies: 5
    Last Post: 18th June 2010, 20:54
  4. extern class
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2007, 22:08
  5. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 20:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.