PDA

View Full Version : how to add a linux C compiled object file to my qt object ?



saman_artorious
12th May 2012, 12:47
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.

: )

amleto
12th May 2012, 14:34
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++

ChrisW67
13th May 2012, 08:09
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:

The object file containing My_Serial_ClosePort() has not been included in the linker command line
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.:


LIBS += file1.o file2.o file3.o


You fix 2. by adding the library to the LIBS variable, e.g.


LIBS += -L/path/to/folder/containing/library -llibraryname
or
LIBS += /path/to/folder/containing/library/libraryname.lib (or .a)

saman_artorious
13th May 2012, 09:49
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.

ChrisW67
13th May 2012, 10:14
Try:


OBJECTS += f1.o f2.o f3.o f3.o

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:


// Pro file
TEMPLATE = app
SOURCES += main.cpp hello.c

// hello.h
extern "C" {
int hello();
}

// hello.c
int hello() {
return 42;
}

// main.cpp
#include <QtCore>
#include <QDebug>

#include "hello.h"

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
qDebug() << hello() << flush;

return 0;
}

works fine, with qmake using gcc for the C file and g++ for the cpp file.

amleto
13th May 2012, 11:45
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.



// hello.h
int hello();


// hello.c
int hello() {
return 42;
}

// main.cpp
#include <QtCore>
#include <QDebug>

extern "C" {
#include "hello.h"
}
...