Results 1 to 7 of 7

Thread: Mixing C and Qt

  1. #1
    Join Date
    Feb 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Mixing C and Qt

    Hi,

    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:
    Qt Code:
    1. ApiCode.c:207: warning: ‘an_FPAA1_InputCell1_IOCell’ initialized and declared ‘extern’
    2. ApiCode.c:218: warning: ‘an_FPAA1_InputCell2_IOCell’ initialized and declared ‘extern’
    3. ApiCode.c:229: warning: ‘an_FPAA1_InputCell3_IOCell’ initialized and declared ‘extern’
    4. ApiCode.c:240: warning: ‘an_FPAA1_InputCell4_IOCell’ initialized and declared ‘extern’
    5. ApiCode.c:251: warning: ‘an_FPAA1_OutputCell1_IOCell’ initialized and declared ‘extern’
    6. ApiCode.c:262: warning: ‘an_FPAA1_OutputCell2_IOCell’ initialized and declared ‘extern’
    7. ApiCode.c:273: warning: ‘an_FPAA1_filt0_0_Capacitor’ initialized and declared ‘extern’
    8. ApiCode.c:286: warning: ‘an_FPAA1_filt1_0_Capacitor’ initialized and declared ‘extern’
    9. ApiCode.c:299: warning: ‘an_FPAA1_filt2_1_Capacitor’ initialized and declared ‘extern’
    10. ApiCode.c:312: warning: ‘an_FPAA1_filt3_1_Capacitor’ initialized and declared ‘extern’
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. an_SetBQLowPassFilterI(6, 0.200, 1, 1.30651);
    2. an_SetBQLowPassFilterI(7, 0.200, 1, 1.30651);
    To copy to clipboard, switch view to plain text mode 

    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.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mixing C and Qt

    "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.

  3. #3
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C and Qt

    Quote Originally Posted by forloRn_
    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.

  4. #4
    Join Date
    Feb 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C and Qt

    This is a screenshot of the debugging session in KDevelop. It says it cannot access memory at address 0x0. Any ideas?

  5. #5
    Join Date
    Feb 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C and Qt

    And this is a screenshot of the an_GrowReconfigData-method that's called right before the line where the segmentation fault occurs.

    Qt Code:
    1. pInfo->data = (an_Byte*) realloc(pInfo->data, pInfo->capacity);
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mixing C and Qt

    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...
    Last edited by wysota; 20th February 2006 at 19:29.

  7. #7
    Join Date
    Feb 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mixing C and Qt

    I forgot to call a method at the beginning of my program that initialised data in the first place.

Similar Threads

  1. Mixing character encodings with QSql
    By Barnacle in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2014, 11:08
  2. Mixing QRegExpValidator and input mask and QLineEdit
    By abernat in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2009, 00:04
  3. Problem when mixing openGL and QPainter
    By sanjayshelke in forum Qt Programming
    Replies: 9
    Last Post: 21st March 2008, 12:49
  4. Mixing MFC and Qt
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2006, 14:55
  5. Replies: 11
    Last Post: 24th March 2006, 06:40

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.