Results 1 to 18 of 18

Thread: Qt with C?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Qt with C?

    I don't know what the precise rules are, but in my limited experience
    you can write some C in a C++ program provided you compile it as
    C++ and not as C. Strings are a problem, though, don't mix C char
    arrays and C++ strings.

    But I've just written a bit of C in Qt :

    FILE * logFile;
    logFile = fopen(filename, "w");
    etc,
    where filename is of type QString and it worked fine.

    Mariane

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Qt with C?

    I don't know what the precise rules are, but in my limited experience
    you can write some C in a C++ program provided you compile it as
    C++ and not as C.
    Strings are a problem, though, don't mix C char
    arrays and C++ strings.
    When you compile C code as C++, then you have a C++ project.
    But if you want to include *.c files or lets say import C compiled elements from a C library, then you are in trouble if you don't deal with the different signatures.

    FILE * logFile;
    logFile = fopen(filename, "w");
    etc,
    where filename is of type QString and it worked fine.
    Are you sure about this?
    fopen() doesnt know QString, so this is impossible, unless there is more to the code then you showed here.
    Could you show the full relevant code?

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: Qt with C?

    Are you sure about this?
    fopen() doesnt know QString, so this is impossible, unless there is more to the code then you showed here.
    I've never tried Qt3 but there may be an implicit conversion to char* :
    Qt Code:
    1. QString::operator char* ()
    2. { ... }
    To copy to clipboard, switch view to plain text mode 

    Anyway it's true that a C++ compiler handles lots of stuff in different way than a C one but code should be still compatible (maybe a few warnings if some type conversions are ambiguous, errors shouldn't come if your code isn't a damn spaghetti plate!!! )
    Personnally I tried such a thing (I mean using C inside C++); I wrote a full file processing API using OOP and so on but the core used C (fopen, fgets, strcmp, ...) cuz, in my opinion, STL sucks and moreover, under mingw32, it makes your program bigger!)
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Qt with C?

    Ok, I guess I am articulating my self poorly.
    So, instead read the following, to understand what I meant in my previous posts:
    http://en.wikipedia.org/wiki/Name_mangling
    Pay attantion to the sections:
    Name mangling in C++
    Handling of C symbols when linking from C++

    And this is not bad about mixed C and C++ code:
    http://developers.sun.com/prodtech/c...es/mixing.html
    And relevant to this thread, is the section: Accessing C++ Code From Within C Source

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Qt with C?

    I've never tried Qt3 but there may be an implicit conversion to char* :
    Not that I can see....
    http://doc.trolltech.com/3.3/qstring.html
    You have these methods for C like strings:
    # const char * ascii () const
    # const char * latin1 () const

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Qt with C?

    Quote Originally Posted by high_flyer
    Not that I can see....
    How about this one: http://doc.trolltech.com/3.3/qstring...r-const-char-*?

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Qt with C?

    I stand corrected.
    I forgot to look in the "List of all member functions".
    Sorry Mariane.

  8. #8
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    Where I work, we have a lot of legacy C code that needs to be used in a QT project. As part of our coding standard, however, we have a rule that states that _any_ C header file must be framed by:

    Qt Code:
    1. #ifdef __cplusplus
    2. extern "C" {
    3. #endif
    To copy to clipboard, switch view to plain text mode 

    and at the end of the header

    Qt Code:
    1. #ifdef __cplusplus
    2. }
    3. #endif
    To copy to clipboard, switch view to plain text mode 

    to ensure that we don't run in to problems with the different name mangling between C and C++. That wouldn't leave your original program untouched, I'm guessing, but it would avoid some of the potential problems of mixing the two.

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
  •  
Qt is a trademark of The Qt Company.