I've created a Plain C project in Qt Creator in Linux, but I'm getting this error:
error: 'for' loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code
What should I put in my *.pro file to enable C99?
QMAKE_CFLAGS += -std=c99
This worked for me, even though it is not documented and the intellisense doesn't recognize it.
You could set QMAKE_CC or QMAKE_CFLAGS_DEBUG and QMAKE_CFLAGS_RELEASE to contain -std=c99, e.g. add into your .pro file
QMAKE_CC= gcc -std=c99
Related
I am trying to force CMake to pass std=gnu17 instead of std=c17 to GCC.
I tried:
set_property(TARGET "${MyProject_${Compile_Type}_NAME}" PROPERTY C_STANDARD gnu17)
in the CMakeLists.txt but I get the following error:
CMake Error at CMakeLists.txt:260 (add_executable):
C_STANDARD is set to invalid value 'gnu17'
The same happens when I try gnu11 instead of gnu17.
The reason I want it to pass the flag is that I am using one of the GCC extensions and that produces a warning when using -pedantic warning levels (which I would prefer to keep using).
Please notice this is specifically about C, not C++.
Usually, CMake will use -std=gnu17 with plain C_STANDARD 17.
But if you want to be sure that CMake will use compiler-specific extensions, then you may set also C_EXTENSIONS property:
set_target_properties(<...> PROPERTIES
C_STANDARD 17
C_EXTENSIONS ON
)
If your code works only with a specific compiler and its extentions, then you probably want to also check a compiler:
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "The project requires g++ to be used as C++ compiler"
endif()
A function, before being used, needs to be declared either in a included header or otherwise (though not a good practice). The header file describes the functions and variables that may be found in a library or an object file and the compiler can create (as of yet) unreferenced symbols to be resolved later whilst linking.
However, my compiler (gcc based toolchain called esp-open-sdk (xtensa CPU)) continues despite finding no reference to a function in the headers and only in the linking stage does the linker intimate of an `undefined reference to <-function-name->'.
Another strange behaviour is that the compiler says nothing if there is no return statement, and the function is not "void".
My question is: What is causing this behaviour? I think it's unlikely but is it some compiler flag?
My compiler flags are:
CFLAGS = -Os -g -O2 -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH
and the linker flags are:
LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static
Probably you use an old version of gcc. Before version 5.x, gcc defaults to a non-standard called "gnu90", which is a non-standard version of the obsolete C90 standard. There is no reason to ever use gnu90 unless you are maintaining some old Linux code. To avoid this, compile with:
-std=c11 -pedantic-errors
-std=c11 meaning you want the compiler to use the current C standard, without involving gnus or other strange animals. -pedantic-errors means that you want it to actually follow the standard and not just pretend to do it.
In order to get the warning for no return from functions, you need to use the option -Wreturn-type, which is included by using -Wall. Always compile with
-Wall -Wextra
Note that "Wall" does not stand for "all warnings", as it leads you to believe. Rather, -Wall means a fistful of warnings and -Wextra means a few warnings more.
I have a automake enabled project which I would like to compile with clang. I have added a configure option to enable clang:
AC_ARG_ENABLE([clang],
[AS_HELP_STRING([--enable-clang],[use clang instead of gcc as C compiler.])])
#Use C99 compilation mode
if test "x$enable_clang" = "xyes"; then
# clang uses c99 mode by default, so we just set CC to clang and we are done
CC="clang";
else
# We do not need to set CC as the default is gcc, but we need to set it to
# use C99 compilation mode
CFLAGS="$CFLAGS -std=c99";
fi
Further up in the file I have also the following two macros, to enable gettext functionality:
AM_GNU_GETTEXT_VERSION([0.18.1])
AM_GNU_GETTEXT([external])
If these two macros are present then configure ignores that the CC variable is set to clang and falls back to gcc. I have to comment out the gettext macros and then clang is used.
Obviously there is some problem with GETTEXT and clang. Am I using the wrong macro, or is clang not able to use the gnu gettext library? How can I fix this?
Have you tried to set CC to clang before you check for libraries (like gettext)? – Some programmer dude
Nice that worked. – lanoxx
I need to compile a code written in C using gcc (in Xcode) with flag options -ansi -pedantic. I know how to change the compiler to gcc, however I don't know how to change the flags.
Thanks in advance.
Use the Other C Flags build setting to add compiler flags that aren't available in Xcode's build settings editor.
I've recently learned how to program simple character drivers and while playing around with the code I noticed that I get a lot of the following GCC warnings thrown for my C99 code:
warning: ISO C90 forbids mixed declarations and code
I assume this is because the main Linux kernel Makefile is set to compile using a non-C99 standard. I searched around I found this answer here on stackoverflow: How to use make and compile as C99?
So I naturally tried the following in my Makefile:
ccflags-y := -std=gnu99
Unfortunately this didn't silence the GCC warnings. I checked the verbose output of make and verified that GCC is indeed executed with the -std=gnu99 tacked on at the end; so I'm a bit confused.
How do I properly compile a Linux kernel module using the -std=gnu99 option?
EDIT:
I noticed the GCC output shows this option: -Wdeclaration-after-statement. Is this why I am getting the warnings even with the -std=gnu99 option?
It turns out that -std=gnu99 does in fact work; I began seeing errors regarding C99 features after removing the compiler flag. So that meant something else was causing the warnings to print out besides the -std= option.
After parsing through the verbose output via make V=1, I discovered the -Wdeclaration-after-statement option as part of the GCC execution. This was the cause of the ISO C90 mixed declaration warnings I saw.
To disable the ISO C90 warnings, pass this to GCC: -Wno-declaration-after-statement.
For example:
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
You can also specify the flag in your Makefile, if you have one:
FLAGS=-std=gnu99