Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use c++ library in .c file
Forum Updated to NodeBB v4.3 + New Features

How to use c++ library in .c file

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BriFuture
    wrote on last edited by
    #1

    There is a Test.c File in my project.
    Contents of Test.c:

    #include <iostream>
    

    Using Qt Creator to compile it, I will get the following errors:

    0_1567668416583_18b3436b-f5bb-4ea2-83ba-8bef0474a81b-image.png

    I do think it's compilers error but I don't know how to solve it, because Any Qt Headers will need c++ compiler.

    When inputing #include <QDebug>, following errors occured:

    0_1567668541763_6b463270-798b-417b-8f1a-8a55b84f1a6a-image.png

    I want to treat .c file as c++ file, erros could not be eliminated unless I change the suffix .c to .cpp.

    JonBJ 1 Reply Last reply
    0
    • B BriFuture

      There is a Test.c File in my project.
      Contents of Test.c:

      #include <iostream>
      

      Using Qt Creator to compile it, I will get the following errors:

      0_1567668416583_18b3436b-f5bb-4ea2-83ba-8bef0474a81b-image.png

      I do think it's compilers error but I don't know how to solve it, because Any Qt Headers will need c++ compiler.

      When inputing #include <QDebug>, following errors occured:

      0_1567668541763_6b463270-798b-417b-8f1a-8a55b84f1a6a-image.png

      I want to treat .c file as c++ file, erros could not be eliminated unless I change the suffix .c to .cpp.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @brifuture
      Is your question that the code you show should be legal for a C program (it is not), or is it that you want to stick with the extension being .c but have the compiler compile it as C++ (like it would for .cpp)? If it's the latter, then you don't say anything about the compiler you are using, you will (presumably) need to specify an option on its command-line somewhere if the compiler supports that.

      B 1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        https://stackoverflow.com/questions/199418/using-c-library-in-c-code

        It is easier to make a C++ project and use c func calls as extern by changing .c to .cpp

        B 1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          to force .c to compile as c++ you can use the -x option
          (if using gcc)

          -x language
          Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are:
          
          c  c-header  cpp-output
          c++  c++-header  c++-cpp-output
          objective-c  objective-c-header  objective-c-cpp-output
          objective-c++ objective-c++-header objective-c++-cpp-output
          assembler  assembler-with-cpp
          ada
          d
          f77  f77-cpp-input f95  f95-cpp-input
          go
          brig
          
          B 2 Replies Last reply
          2
          • JoeCFDJ JoeCFD

            https://stackoverflow.com/questions/199418/using-c-library-in-c-code

            It is easier to make a C++ project and use c func calls as extern by changing .c to .cpp

            B Offline
            B Offline
            BriFuture
            wrote on last edited by
            #5

            @JoeCFD
            Thank you.
            But that would be much helper if the file suffix coule stay unchanged.
            I believe a .sh or .bat script could do that for me. But it still seems a little troublesome.

            1 Reply Last reply
            0
            • JonBJ JonB

              @brifuture
              Is your question that the code you show should be legal for a C program (it is not), or is it that you want to stick with the extension being .c but have the compiler compile it as C++ (like it would for .cpp)? If it's the latter, then you don't say anything about the compiler you are using, you will (presumably) need to specify an option on its command-line somewhere if the compiler supports that.

              B Offline
              B Offline
              BriFuture
              wrote on last edited by
              #6

              @JonB
              Thank you,
              To be an honest, I do not care what compiler the file should be processed with.

              I just want to share some codes as a common part between my qt project and mplab project. (Some mathmatical part code, so it would be nice if the codes could be debugged on pc and then put into the MCU).

              While x16 compiler is used as the compiler in the mplab project , so I have to change the behaviour of the shared code with qt project, So any changes I do in qt could be applied into mplab project, and the same inversely.

              1 Reply Last reply
              0
              • mrjjM mrjj

                Hi
                to force .c to compile as c++ you can use the -x option
                (if using gcc)

                -x language
                Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are:
                
                c  c-header  cpp-output
                c++  c++-header  c++-cpp-output
                objective-c  objective-c-header  objective-c-cpp-output
                objective-c++ objective-c++-header objective-c++-cpp-output
                assembler  assembler-with-cpp
                ada
                d
                f77  f77-cpp-input f95  f95-cpp-input
                go
                brig
                
                B Offline
                B Offline
                BriFuture
                wrote on last edited by
                #7

                @mrjj
                Thank you.
                I'm using cl.exe (msvc 2015), so the option -x seems effectless.
                I will google some cl options to achieve that.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  to force .c to compile as c++ you can use the -x option
                  (if using gcc)

                  -x language
                  Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are:
                  
                  c  c-header  cpp-output
                  c++  c++-header  c++-cpp-output
                  objective-c  objective-c-header  objective-c-cpp-output
                  objective-c++ objective-c++-header objective-c++-cpp-output
                  assembler  assembler-with-cpp
                  ada
                  d
                  f77  f77-cpp-input f95  f95-cpp-input
                  go
                  brig
                  
                  B Offline
                  B Offline
                  BriFuture
                  wrote on last edited by
                  #8

                  @mrjj
                  Wow, that's working! With the following statements in my .pro file
                  msvc: QMAKE_CFLAGS += /TP
                  the troublesome problem is solved.

                  Pablo J. RoginaP 1 Reply Last reply
                  1
                  • B BriFuture

                    @mrjj
                    Wow, that's working! With the following statements in my .pro file
                    msvc: QMAKE_CFLAGS += /TP
                    the troublesome problem is solved.

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @BriFuture said in How to use c++ library in .c file:

                    the troublesome problem is solved.

                    So please mark your post as such! Thanks.

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved