I just learned that inline functions can be defined in another function's body as well.
I'm using the mpicc compiler and the following code compiles successfully without warnings and errors:
#include <stdio.h>
int main() {
inline int inlinetest(int x) {
return x * 4;
}
printf("%d\n", inlinetest(8));
return 0;
}
However, CLion shows the following errors within the file:
A the {
bracket of inlinetest
: Function definition is not allowed here
Where I try to call inlinetest
: Implicit declaration of function 'inlinetest' is invalid in C99
I tried setting the CMAKE_C_STANDARD
and C_STANDARD
to 11, but this doesn't make a difference.
Is there a way to configure CMake or CLion itself, such that it doesn't detect this "error"?
As I'm sure you're aware, your mpicc
compiler is a wrapper for a real
compiler, which is your case apparently is gcc
.
In your example you have defined inlinetest
as a nested function within main
.
Nested functions are a language extension in GNU C They are not supported by Standard C.
gcc
will compile your program clean with default options:
$ cat main.c
#include <stdio.h>
int main() {
inline int inlinetest(int x) {
return x * 4;
}
printf("%d\n", inlinetest(8));
return 0;
}
$ gcc main.c
$ ./a.out
32
but gcc
will complain if you direct it to enforce any C standard from the oldest:
$ gcc -std=c89 -pedantic main.c
main.c: In function ‘main’:
main.c:4:5: error: ‘inline’ undeclared (first use in this function)
4 | inline int inlinetest(int x) {
| ^~~~~~
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:11: error: expected ‘;’ before ‘int’
4 | inline int inlinetest(int x) {
| ^~~~
| ;
to the latest:
$ gcc -std=c18 -pedantic main.c
main.c: In function ‘main’:
main.c:4:5: warning: ISO C forbids nested functions [-Wpedantic]
4 | inline int inlinetest(int x) {
| ^~~~~~
I'm not familiar with CLion, but IDEs that boast on-the-fly code analysis always harbour the potential nuisance that the on-the-fly analyser will disagree with the compiler on some points. The IDE will probably give you some way to review the analyser's rules catalog and tweak it to silence nuisance diagnostics. But I would not hope you can get CLion to recognize GNU C nested functions: they are arcane.
And for that reason I suggest that you don't pick this fight. You surely haven't found a compelling reason to make you code rely on support for GNU C nested functions, so just don't bother with them.