Results 1 to 6 of 6

Thread: how to add a linux C compiled object file to my qt object ?

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to add a linux C compiled object file to my qt object ?

    hi

    I am writing an object in qt by adding it's related C code.
    The compiler pops up the following errors when it calls the C functions in the header files:
    error: undefined reference to `My_Serial_ClosePort(My_BoardDescriptor*)'
    The point is that, these functions are defined in the .c files and their relative .o Compiled Object Files have been created as well.
    Do you think it's better to add it statically to my program using qmake options, if of course possible!
    or convert the .c file to .cpp and add them to my qt project.

    As I am working under linux, I would thank if you also mention how to add the object files to qmake , if ever possible.

    : )

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to add a linux C compiled object file to my qt object ?

    the data in the .o file will have non-mangled class/method names. I'm not sure if that is compatible with c++ .o files.

    I would imagine it will be a lot easier to just change the .c to .cpp and compile it as c++
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to add a linux C compiled object file to my qt object ?

    Quote Originally Posted by saman_artorious View Post
    The compiler pops up the following errors when it calls the C functions in the header files:
    error: undefined reference to `My_Serial_ClosePort(My_BoardDescriptor*)'
    No, the linker has generated that error message because either:
    1. The object file containing My_Serial_ClosePort() has not been included in the linker command line
    2. The library containing the My_Serial_ClosePort() routine has not been included in the linker command line


    You fix 1 by adding the *.c sources to your SOURCES variable and building it into your program. If the object files have been produced separately you may be able to use them by listing each one of them in LIBS e.g.:
    Qt Code:
    1. LIBS += file1.o file2.o file3.o
    To copy to clipboard, switch view to plain text mode 

    You fix 2. by adding the library to the LIBS variable, e.g.
    Qt Code:
    1. LIBS += -L/path/to/folder/containing/library -llibraryname
    2. or
    3. LIBS += /path/to/folder/containing/library/libraryname.lib (or .a)
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to add a linux C compiled object file to my qt object ?

    There is no library associated with my work, so, it's definitely the first reason.

    - qmake -project
    - added LIBS += f1.o f2.o f3.o f3.o to the .pro project file

    -now, qmake and finally make

    I get the same error notifications even when the object files are are added to the .pro file.

    I did try the amleto's way, it worked out, but I also want to know how to solve it using your method Chris.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to add a linux C compiled object file to my qt object ?

    Try:
    Qt Code:
    1. OBJECTS += f1.o f2.o f3.o f3.o
    To copy to clipboard, switch view to plain text mode 
    although I think that, if you have the source, building it into your app directly or via a library is a better option.

    You should not need to rename the files to include them into your project. For example this project:
    Qt Code:
    1. // Pro file
    2. TEMPLATE = app
    3. SOURCES += main.cpp hello.c
    4.  
    5. // hello.h
    6. extern "C" {
    7. int hello();
    8. }
    9.  
    10. // hello.c
    11. int hello() {
    12. return 42;
    13. }
    14.  
    15. // main.cpp
    16. #include <QtCore>
    17. #include <QDebug>
    18.  
    19. #include "hello.h"
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QCoreApplication app(argc, argv);
    24. qDebug() << hello() << flush;
    25.  
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 
    works fine, with qmake using gcc for the C file and g++ for the cpp file.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to add a linux C compiled object file to my qt object ?

    It's probably worth pointing out that if your .c files already have their own .h file (why wouldn't they?), then you would put extern"C"{...} around the #include for the C header.

    Qt Code:
    1. // hello.h
    2. int hello();
    3.  
    4.  
    5. // hello.c
    6. int hello() {
    7. return 42;
    8. }
    9.  
    10. // main.cpp
    11. #include <QtCore>
    12. #include <QDebug>
    13.  
    14. extern "C" {
    15. #include "hello.h"
    16. }
    17. ...
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 15:49
  2. Replies: 1
    Last Post: 8th November 2011, 22:27
  3. Replies: 4
    Last Post: 15th July 2011, 18:31
  4. Replies: 4
    Last Post: 20th August 2010, 13:07
  5. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 14:28

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.