Use C99 extention in Qt Creator
-
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?
-
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.
-
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.
-
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. -
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON)
-
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.@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
andC_EXTENSIONS
, neither works. -
@kuzulis Thank you for your reply! Unfortunately it does not work.
-
@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
andC_EXTENSIONS
, neither works.@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.
-
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.
-
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.
@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?
-
@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?
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.