Gcc compiling invalid C code

Some of my C programs do not work as expected. For example, passing a link is not possible in C, but when I write a C program that uses this and compiles it with gcc, it works fine.

Is gcc a C ++ compiler? How to make it behave like a C compiler?

+3
source share
6 answers

gcc, g ++, and other interfaces use file names to define the language. For example, the only significant difference between gcc and g ++ is the one that bites new C ++ programmers: different link settings (for C ++ stdlib).

-x (, , -std), , . , gcc . C *.c.

, / *.C, ; ++.

+10

:

int f( int & r ) {
    return r + 1;
}

int main() {
    int x = 3;
    return f( x );
}

:

 gcc e.c

:

 e.c:1: error: expected ';', ',' or ')' before '&' token

, , .cpp? , gcc- ++.

+7

gcc - , C, ++, Ada, Fortran, Java , , , , .

, , C, C ++, ++. C, -x c .

, C , C ++.

+5

-pedantic C, , . --std=c99 C99, --std=c89 C89; , .

: , -ansi C89, ++ 98 , "C-mode".

+4

gcc - . -x.

g++ ++ , (*.c) ( ).

:

echo "int main() { } " > test.c

gcc -v -c test.c

[, /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus, front-end.]

========================

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -quiet -v test.c -quiet -dumpbase test.c -mtune=generic -auxbase test -version -o /tmp/ccUiF4Qr.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.1.2/include
 /usr/include
End of search list.
GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46) (i386-redhat-linux)
    compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46).
GGC heuristics: --param ggc-min-expand=59 --param ggc-min-heapsize=55455
Compiler executable checksum: 435964263b657ac05d988fae7b6714b1
 as -V -Qy -o test.o /tmp/ccUiF4Qr.s
GNU assembler version 2.17.50.0.6-12.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-12.el5 20061020

=============================

mv test.c test.cpp
gcc -v -c test.cpp

=============================

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus -quiet -v -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -auxbase test -version -o /tmp/ccUgae0u.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2
 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux
 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.1.2/include
 /usr/include
End of search list.
GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-46) (i386-redhat-linux)
    compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46).
GGC heuristics: --param ggc-min-expand=59 --param ggc-min-heapsize=55455
Compiler executable checksum: 2c84476b74368e297382b43d14e53b01
 as -V -Qy -o test.o /tmp/ccUgae0u.s
GNU assembler version 2.17.50.0.6-12.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-12.el5 20061020

================================

mv test.cpp test.c
g++ -v -c test.c

You will get the same result as gcc+ test.cpp, which means compiled as C ++.

ccis the front of C cc1plusis the front end of C ++. What is it.

+2
source

g++should be front-end for C ++ and ccfor C, but both point to gcc.

If you want to compile standard C code, use gcc -ansi

0
source

Source: https://habr.com/ru/post/1731890/


All Articles