原文: http://blog.youkuaiyun.com/tianwailaibin/article/details/6919133
在根录目gcc文件夹下新建文本文件:hello.cpp
内容如下:
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
应用程序->附件->终端:
[root@localhost ~]# cd gcc
执行编译命令:
[root@localhost gcc]# gcc -c hello.cpp -o hello
运行:
[root@localhost gcc]# ./hello
bash: ./hello: 权限不够
查看权限:
[root@localhost gcc]# ls -l
总计 16
-rw------- 1 root root 77 10-30 16:33 hello.cpp
-rw------- 1 root root 77 10-30 16:33 新文件~
重新生成一个代扩展名的(扩展名对LINUX来说不重要)
[root@localhost gcc]# gcc -c hello.cpp -o hello.o
[root@localhost gcc]# ./hello.o
bash: ./hello.o: 权限不够
给hello.o修改权限:
[root@localhost gcc]# chmod 777 hello.o
再次执行:
[root@localhost gcc]# ./hello.o
bash: ./hello.o: cannot execute binary file
输出编译细节:
[root@localhost gcc]# gcc -v hello.cpp
使用内建 specs。
目标:i386-redhat-linux
配置为:../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
线程模型:posix
gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-48)
/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus -quiet -v -D_GNU_SOURCE hello.cpp -quiet -dumpbase hello.cpp -mtune=generic -auxbase hello -version -o /tmp/ccp3FAlX.s
忽略不存在的目录“/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include”
#include "..." 搜索从这里开始:
#include <...> 搜索从这里开始:
/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
搜索列表结束。
GNU C++ 版本 4.1.2 20080704 (Red Hat 4.1.2-48) (i386-redhat-linux)
由 GNU C 版本 4.1.2 20080704 (Red Hat 4.1.2-48) 编译。
GGC 准则:--param ggc-min-expand=98 --param ggc-min-heapsize=128356
Compiler executable checksum: e1c6e7e253b2b6b8ac8ef94463a3ee70
as -V -Qy -o /tmp/ccK5uLEO.o /tmp/ccp3FAlX.s
GNU assembler version 2.17.50.0.6-14.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14.el5 20061020
/usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. /tmp/ccK5uLEO.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
/tmp/ccK5uLEO.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld 返回 1
以上输出内容中:undefined reference to `__gxx_personality_v0' 因该是个出错信息!
在网上搜了一下发现答案:
--------------------------------------------
7楼 ExcaliburXK 2011-09-03 15:53发表 [回复] [引用] [举报]
gcc通过文件的后缀名来决定如何处理该文件
gcc把.c当成c语言源代码文件处理
把.C或.cc当成c++源代码处理
默认是不支持.cpp文件的,改下文件后缀或者在编译命令行中用选项指定按C++文件处理就OK了。
6楼 yankai0219 2011-08-23 08:28发表 [回复]
昨晚没有解决这个问题,早上搜到,问题的原因是因为
由于源代码的文件后缀名和代码的语法不相符合,比如用C的语法的文件,选择了cpp这样的后缀名(或者<大写C>这样的后缀名)”。解决方法是:将.cpp的后缀名改成.c 就可以解决问题。
--------------------------------------------
把hello.cpp改为 hello.c重新编译:
[root@localhost gcc]# gcc -v hello.c -o hello.o
使用内建 specs。
目标:i386-redhat-linux
配置为:../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
线程模型:posix
gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-48)
/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -mtune=generic -auxbase hello -version -o /tmp/ccFQDBub.s
忽略不存在的目录“/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include”
#include "..." 搜索从这里开始:
#include <...> 搜索从这里开始:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.2/include
/usr/include
搜索列表结束。
GNU C 版本 4.1.2 20080704 (Red Hat 4.1.2-48) (i386-redhat-linux)
由 GNU C 版本 4.1.2 20080704 (Red Hat 4.1.2-48) 编译。
GGC 准则:--param ggc-min-expand=98 --param ggc-min-heapsize=128356
Compiler executable checksum: 2da9a5b9b7fcbb90f7b2463be5d65b32
as -V -Qy -o /tmp/ccedgPGe.o /tmp/ccFQDBub.s
GNU assembler version 2.17.50.0.6-14.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14.el5 20061020
/usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o hello.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. /tmp/ccedgPGe.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
以上内容就没有了出错信息,
于是运行:
[root@localhost gcc]# ./hello.o
Hello world!
好终于出来了!阿弥陀佛!
[root@localhost gcc]#