View Full Version : Mixing C and Qt

20th February 2006, 15:48

A friend of mine has asked me to create a nice GUI to go along with his C-application. It's an application that's capable of reconfiguring an Anadigm-chip, but that's besides the point.

The C-application compiles and runs without a problem. My friend wrote the main program himself, but the rest of the files (ApiCode.h, ApiCode.c, CAMCode.h, CAMCode.c) were generated by an Anadigm-tool.

I assumed I could keep the Anadigm files, and only had to rewrite the main program using Qt. I compiled the program in the console with qmake -project, qmake and make. This time around, the compiler gave the following warnings:

ApiCode.c:207: warning: ‘an_FPAA1_InputCell1_IOCell’ initialized and declared ‘extern’
ApiCode.c:218: warning: ‘an_FPAA1_InputCell2_IOCell’ initialized and declared ‘extern’
ApiCode.c:229: warning: ‘an_FPAA1_InputCell3_IOCell’ initialized and declared ‘extern’
ApiCode.c:240: warning: ‘an_FPAA1_InputCell4_IOCell’ initialized and declared ‘extern’
ApiCode.c:251: warning: ‘an_FPAA1_OutputCell1_IOCell’ initialized and declared ‘extern’
ApiCode.c:262: warning: ‘an_FPAA1_OutputCell2_IOCell’ initialized and declared ‘extern’
ApiCode.c:273: warning: ‘an_FPAA1_filt0_0_Capacitor’ initialized and declared ‘extern’
ApiCode.c:286: warning: ‘an_FPAA1_filt1_0_Capacitor’ initialized and declared ‘extern’
ApiCode.c:299: warning: ‘an_FPAA1_filt2_1_Capacitor’ initialized and declared ‘extern’
ApiCode.c:312: warning: ‘an_FPAA1_filt3_1_Capacitor’ initialized and declared ‘extern’

Those variables are declared 'extern' everywhere: I can't seem to find a regular declaration. Why is this okay with the C-compiler, while the C++-compiler issues warnings?

Anyway, the program compiled successfully. It runs perfectly, until I press the button "Genereer stream" ("Generate stream"): the program crashes with a segmentation fault. Experience has taught me that these things happen when you try to access a variable which hasn't been assigned memory. Is there a connection with the warnings above?

Apparently, the lines in anadigmform.cpp that are causing the crash are the following:

an_SetBQLowPassFilterI(6, 0.200, 1, 1.30651);
an_SetBQLowPassFilterI(7, 0.200, 1, 1.30651);

In short: I've given myself a headache looking for an explanation, but I can't seem to find a reason for the crashing. Does anyone have an idea? And can I mix C and Qt like this?

You can find the files here (http://users.telenet.be/forlorn/anadigm.zip).


20th February 2006, 16:40
"mix C and Qt" --- this is misleading by itself. There is no language called "Qt". Qt uses (by default) C++, so it's a question of mixing C and C++ code which is of course possible.

As for your problem, try using a debugger and see the backtrace of the application after it crashes. This way no guessing is needed, the debugger will tell you which call failed. Just remember to compile in debug mode.

20th February 2006, 16:41
The C-application compiles and runs without a problem.
I don't know which compiler do you use to compile pure C programms, but gcc -c ApiCode.c gives the same warnings for me.

20th February 2006, 19:47
This (http://users.telenet.be/forlorn/debugged.png) is a screenshot of the debugging session in KDevelop. It says it cannot access memory at address 0x0. Any ideas?

20th February 2006, 19:58
And this (http://users.telenet.be/forlorn/an_GrowReconfigData.png) is a screenshot of the an_GrowReconfigData-method that's called right before the line where the segmentation fault occurs.

pInfo->data = (an_Byte*) realloc(pInfo->data, pInfo->capacity);

I think pInfo->data somehow ends up as 0x0 after the call to realloc. Could this be? And do you have a solution?

20th February 2006, 20:27
Based on the first screenshot I take it that "pInfo" of pInfo->data is null. A full backtrace would be nicer though... especially one from a program compiled with -ggdb.

BTW. You should check the result of realloc...

21st February 2006, 01:56
I forgot to call a method at the beginning of my program that initialised data in the first place. :o