Results 1 to 4 of 4

Thread: Compiling .c files using C++ and QT

  1. #1
    Join Date
    Sep 2010
    Posts
    2
    Platforms
    Unix/X11 Windows

    Default Compiling .c files using C++ and QT

    i have a full application running and would to know if and how i open a new project (What type) and how to import the files in (.h, .c and make files)
    finally how to set the compiler to a C compiler or the files to C++.

    im new with QT.

    Thanks in advance

  2. #2
    Join Date
    Sep 2010
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Compiling .c files using C++ and QT

    In Qt everything is based on a .pro file. This is the project file as well as the basis for the make file. You can type one yourself. Or have a look at qmake to generate one for you based on your files.

    Another, more practical, option is to download and install Qt Creator (Use "Qt4 SDK"). Start a new project, and add your files. All the Qt-specific stuff will be setup automatically and you needn't care about the C/C++ compiler settings either. That will all be taken care of automatically. It even comes with a gcc compiler

    (but to reply to the title: if you want to *force* C++, you could compile with "g++" instead of with "gcc". Just replace the one with the other, and it will be strictly C++ from that point forward. But in Qt projects it is best to let Qt Creator or qmake take care of that for you instead.)

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Compiling .c files using C++ and QT

    @koder: Qt SDK does not install g++ for you (except the Windows one which comes with MingW) on other platforms you need to install g++ and make.

    And on-topic, g++ (or mingw) got to compile C code not only C++, off course if your code is standard C. (most C code is also C++)

    So i suggest you try to create a project, you can start with a "Empty project" and add your files (.c and .h) to that project and see if you will have any issues.
    After you start, if you run into troubles you can post again, and depending on what you are trying to achieve we may be able to give you better advices.

    NOTE: C++ compilers have the C header files from C standard library, but there are without .h extension and have 'c' prefix: like in place of "#include <stdio.h>" you will write "#include <cstdio>" and so on...

  4. #4
    Join Date
    Sep 2010
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Compiling .c files using C++ and QT

    Quote Originally Posted by Zlatomir View Post
    @koder: Qt SDK does not install g++ for you (except the Windows one which comes with MingW) on other platforms you need to install g++ and make.
    Right, but on most other systems, gcc/g++ is already available. What I meant to say was: Even the compiler tool chain is taken care of automatically.

    Quote Originally Posted by Zlatomir View Post
    NOTE: C++ compilers have the C header files from C standard library, but there are without .h extension and have 'c' prefix: like in place of "#include <stdio.h>" you will write "#include <cstdio>" and so on...
    Yes. But be careful. They are not always exactly the same as there C counterparts. And you'd need to put "std::" in front of the functions. So you either use <stdio.h> and printf *OR* you use <cstdio> and std:rintf.
    You can also solve this by putting "using namespace std;" at the global scope. This is not advised behavior. But since you are trying to get C code compiled in C++, it is an option.

    Also, if you're using C functionality that is differs from C++, you can put an "extern" around it:

    Qt Code:
    1. // C++ code
    2. extern "C"
    3. {
    4. // C code
    5. }
    6. // C++ code
    To copy to clipboard, switch view to plain text mode 

    This may even be required when creating shared libraries for other C-only applications.

Similar Threads

  1. Replies: 2
    Last Post: 1st March 2010, 18:58
  2. Compiling cpp files with qt cmd
    By Noks in forum Newbie
    Replies: 8
    Last Post: 16th January 2010, 20:58
  3. Strange Errors while compiling (QT Header Files)
    By DrDonut in forum Qt Programming
    Replies: 5
    Last Post: 21st May 2009, 14:10
  4. Replies: 0
    Last Post: 23rd April 2009, 10:38
  5. compiling problem / Qt DLL files
    By ht1 in forum Newbie
    Replies: 1
    Last Post: 5th January 2008, 18:58

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.