I accidentally assign .C
[note to Uppercase C ] extension to my C source code in UBUNTU 16.04 LTS and open it with Gedit program to enter my codes.
gcc compiler not recognize it as a C programming source code and produce error when it try to compile.
And UBUNTU file manager show that as a cpp file.
Code
#include <stdlib.h>
int main(){
int * c = malloc(sizeof(int));
free(c);
return 0;
}
gcc compile command, output:
$gcc test.C -o test
test.C:8:18: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
int * c = malloc( sizeof(int) );
As we know this is a C++ specific error, and i think gcc, behave that like a C++ file as said in this .
This is my system info
Linux ee 4.8.0-36-generic #36~16.04.1-Ubuntu SMP i686 i686 i686 GNU/Linux
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
gedit - Version 3.18.3
I know we simply can cast the return value of malloc()
to (int*)
and this will obviously works.
But we know malloc()
return type is void*
see linuxdie malloc()
And it is not true to cast type of malloc()
in C why not cast return of malloc()
Is only lowercase extension valid for C source code in UBUNTU, why they do this?
And how I can fix this and compile my source.C
with gcc on my machine.
Edit
As MCG said in answers, we can force gcc
to treat any given file as specific type which is tell to it with -x
flag.
For example if we have a C valid source code with .f extension or even a source code without any extension on UBUNTU, by using this command it will be compiled correctly,
Compile C source code with any extension:
gcc -x c src.f -o src // with .f or any others
gcc -x c src -o src //without extension