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. Use C99 extention in Qt Creator
QtWS25 Last Chance

Use C99 extention in Qt Creator

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.1k Views
  • 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.
  • T Offline
    T Offline
    tommytrojan
    wrote on last edited by
    #1

    I try to use array designators like so:

    const uchar background[49] = { [0 ... 48] = 236 };
    

    But Qt Creator gives me warnings like warning: Array designators are a C99 extension and the project does not compile. So I try to add in CMakeLists.txt:

    set(CMAKE_C_STANDARD 99)
    

    or

    set_target_properties(mytarget PROPERTIES
      C_STANDARD 99
    )
    

    But neither works. How do I use C99 standard?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What the warning says is that this is an extension to the C99 standard, not that the extension is part of the standard. So if you set your mode to standard C99 you're disabling extensions.

      Try setting C_EXTENSIONS to true and see if that helps.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tommytrojan
        wrote on last edited by
        #3

        Thank you for the reply!

        I tried C_EXTENSIONS but it didn't work. I tried

        set(CMAKE_C_EXTENSIONS TRUE)
        set(CMAKE_CXX_EXTENSIONS TRUE)
        set(CMAKE_C_EXTENSIONS ON)
        set(CMAKE_CXX_EXTENSIONS ON)
        

        or

        set_target_properties(mytarget PROPERTIES
          C_EXTENSIONS ON
        )
        

        etc.... None of these works.

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Is it a .c or .cpp file?
          This is a GNU extension, so will only work with GCC. What compiler are you using?
          If GCC then look at the command line output when you're compiling and see what -std flag is passed to the compiler.
          Also, maybe it should be CMAKE_C_EXTENSIONS. I'm not that good with CMake.

          T 1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #5
            set(CMAKE_C_STANDARD 99)
            set(CMAKE_C_STANDARD_REQUIRED ON)
            
            T 1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              Is it a .c or .cpp file?
              This is a GNU extension, so will only work with GCC. What compiler are you using?
              If GCC then look at the command line output when you're compiling and see what -std flag is passed to the compiler.
              Also, maybe it should be CMAKE_C_EXTENSIONS. I'm not that good with CMake.

              T Offline
              T Offline
              tommytrojan
              wrote on last edited by
              #6

              @Chris-Kawa It is a .cpp file. I'm indeed using GCC. The -std flag shows -std=gnu++17. I'm quite confused... Is this a C extension, or a CPP extension? If it is a C extension, I can still use it in a cpp file, right?

              I tried both CMAKE_C_EXTENSIONS and C_EXTENSIONS, neither works.

              Chris KawaC 1 Reply Last reply
              0
              • K kuzulis
                set(CMAKE_C_STANDARD 99)
                set(CMAKE_C_STANDARD_REQUIRED ON)
                
                T Offline
                T Offline
                tommytrojan
                wrote on last edited by
                #7

                @kuzulis Thank you for your reply! Unfortunately it does not work.

                1 Reply Last reply
                0
                • T tommytrojan

                  @Chris-Kawa It is a .cpp file. I'm indeed using GCC. The -std flag shows -std=gnu++17. I'm quite confused... Is this a C extension, or a CPP extension? If it is a C extension, I can still use it in a cpp file, right?

                  I tried both CMAKE_C_EXTENSIONS and C_EXTENSIONS, neither works.

                  Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  @tommytrojan Unless you specify the language via compiler parameter it will be determined by a file extension. See -x option. If you use .c it will compile as C. If you use .cpp it will compile as C++. You are compiling it as C++.

                  Is this a C extension, or a CPP extension?

                  The syntax you're trying to use is a C language extension. You are compiling this file as C++.

                  If it is a C extension, I can still use it in a cpp file, right?

                  I wouldn't assume that at all. Those are two different languages. C language extensions are, well, for C language.

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    tommytrojan
                    wrote on last edited by
                    #9

                    Thank you for your answer, I now understand it much better than before.

                    I tried almost all suggestions I can find on the web, nothing works. My intuition is to blame CMake for this. I have to give up on this syntax. Maybe it is good not to depend on GNU specific syntax.

                    It is a shame though because I want a const array of a specific value. Do you know another way to achieve it in c++?

                    Part of me cannot believe that it cannot be done in c++ in 2022 without GNU.

                    Chris KawaC 1 Reply Last reply
                    0
                    • T tommytrojan

                      Thank you for your answer, I now understand it much better than before.

                      I tried almost all suggestions I can find on the web, nothing works. My intuition is to blame CMake for this. I have to give up on this syntax. Maybe it is good not to depend on GNU specific syntax.

                      It is a shame though because I want a const array of a specific value. Do you know another way to achieve it in c++?

                      Part of me cannot believe that it cannot be done in c++ in 2022 without GNU.

                      Chris KawaC Online
                      Chris KawaC Online
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @tommytrojan said:

                      Maybe it is good not to depend on GNU specific syntax.

                      By using GNU language extensions you're tying yourself to a GNU compiler that supports them, which is not a great idea in general. Maybe in some cases, but I would say not if it's just for this one small thing.

                      It is a shame though because I want a const array of a specific value.

                      I don't think there's convenient way to do that in C++. Here's one inconvenient way. A helper that you can reuse. It's a compile time function in C++20.

                      template<typename T, int size>
                      constexpr auto make_array(T value)
                      {
                          std::array<T, size> arr;
                          arr.fill(value);
                          return arr;
                      }
                      
                      
                      const auto background = make_array<unsigned char, 49>(236);
                      

                      Btw. Out of curiosity - what do you need a constant array of same value for?

                      T 1 Reply Last reply
                      0
                      • Chris KawaC Chris Kawa

                        @tommytrojan said:

                        Maybe it is good not to depend on GNU specific syntax.

                        By using GNU language extensions you're tying yourself to a GNU compiler that supports them, which is not a great idea in general. Maybe in some cases, but I would say not if it's just for this one small thing.

                        It is a shame though because I want a const array of a specific value.

                        I don't think there's convenient way to do that in C++. Here's one inconvenient way. A helper that you can reuse. It's a compile time function in C++20.

                        template<typename T, int size>
                        constexpr auto make_array(T value)
                        {
                            std::array<T, size> arr;
                            arr.fill(value);
                            return arr;
                        }
                        
                        
                        const auto background = make_array<unsigned char, 49>(236);
                        

                        Btw. Out of curiosity - what do you need a constant array of same value for?

                        T Offline
                        T Offline
                        tommytrojan
                        wrote on last edited by
                        #11

                        Thank you! This template helper can be really useful!

                        @Chris-Kawa said in Use C99 extention in Qt Creator:

                        Btw. Out of curiosity - what do you need a constant array of same value for?

                        Indeed! In the past two hours I thought hard on this and decided to use a const int instead, and construct the needed array later on the fly.

                        Thank you for the follow up! I learned a lot from you.

                        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