【问题标题】:GCC not working but G++ doesGCC 不工作,但 G++ 可以
【发布时间】:2015-12-15 22:30:04
【问题描述】:

我正在尝试在“开箱即用”操作系统上的 BeagleBone Black 上运行一些程序,但在使用 gcc 进行编译时我不断遇到问题。这在尝试编译一个简单的 hello world 程序时得到了最好的说明:

#include <stdio.h>

int main(void)
{
 printf("Hello World\n");
 return 0;
}

编译和运行的输出是:

debian@beaglebone:~$ g++ helloworld.c -o test
debian@beaglebone:~$ ./test
Hello World
debian@beaglebone:~$ gcc helloworld.c -o test
debian@beaglebone:~$ ./test
./test: line 27: typedef: command not found
./test: line 36: typedef: command not found
./test: line 37: typedef: command not found
./test: line 38: typedef: command not found
./test: line 39: typedef: command not found
./test: line 42: typedef: command not found
./test: line 43: typedef: command not found
./test: line 44: typedef: command not found
./test: line 45: typedef: command not found
./test: line 46: typedef: command not found
./test: line 47: typedef: command not found
./test: line 52: __extension__: command not found
./test: line 53: __extension__: command not found
./test: line 61: __extension__: command not found
./test: line 62: __extension__: command not found
./test: line 68: __extension__: command not found
./test: line 69: __extension__: command not found
./test: line 70: __extension__: command not found
./test: line 71: __extension__: command not found
./test: line 72: __extension__: command not found
./test: line 73: __extension__: command not found
./test: line 74: __extension__: command not found
./test: line 75: __extension__: command not found
./test: line 76: __extension__: command not found
./test: line 77: __extension__: command not found
./test: line 78: syntax error near unexpected token `}'
./test: line 78: `__extension__ typedef struct { int __val[2]; } __fsid_t;'
debian@beaglebone:~$

我的 gcc 和 g++ 版本是:

debian@beaglebone:~$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

debian@beaglebone:~$ g++ --version
g++ (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

以前有人遇到过这个问题吗?我环顾四周,但似乎没有一个修复程序适用。提前致谢。

编辑:gcc -v helloworld.c -o outputFile 的输出

debian@beaglebone:~$ gcc -v helloworld.c -o outputFile
Using built-in specs.
COLLECT_GCC=gcc
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)
COLLECT_GCC_OPTIONS='-E' '-v' '-o' 'outputFile' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' '-mtls-dialect=gnu'
 /usr/lib/gcc/arm-linux-gnueabihf/4.7/cc1 -E -quiet -v -imultilib . -imultiarch arm-linux-gnueabihf helloworld.c -o outputFile -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -mthumb -mtls-dialect=gnu
ignoring nonexistent directory "/usr/local/include/arm-linux-gnueabihf"
ignoring nonexistent directory "/usr/lib/gcc/arm-linux-gnueabihf/4.7/../../../../arm-linux-gnueabihf/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/arm-linux-gnueabihf/4.7/include
 /usr/local/include
 /usr/lib/gcc/arm-linux-gnueabihf/4.7/include-fixed
 /usr/include/arm-linux-gnueabihf
 /usr/include
End of search list.
COMPILER_PATH=/usr/lib/gcc/arm-linux-gnueabihf/4.7/:/usr/lib/gcc/arm-linux-gnueabihf/4.7/:/usr/lib/gcc/arm-linux-gnueabihf/:/usr/lib/gcc/arm-linux-gnueabihf/4.7/:/usr/lib/gcc/arm-linux-gnueabihf/
LIBRARY_PATH=/usr/lib/gcc/arm-linux-gnueabihf/4.7/:/usr/lib/gcc/arm-linux-gnueabihf/4.7/../../../arm-linux-gnueabihf/:/usr/lib/gcc/arm-linux-gnueabihf/4.7/../../../:/lib/arm-linux-gnueabihf/:/lib/:/usr/lib/arm-linux-gnueabihf/:/usr/lib/
COLLECT_GCC_OPTIONS='-E' '-v' '-o' 'outputFile' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' '-mtls-dialect=gnu'

【问题讨论】:

  • 尝试调用你的可执行文件而不是test
  • 你确定你没有不小心将-E 输入到 gcc 选项中吗?
  • @user3793489,当您运行gcc 时,文件./test 中的内容是经过预处理的源,就像您在命令行上提供了-E 选项一样。这不是gcc 的正常行为,cmets 的目标是一些似是而非的原因,即您可能会在没有明确指定-E 的情况下获得该行为。由于这没有成功,您应该考虑在特定于 BeagleBone 的支持论坛上进行查询。
  • gcc -v helloworld.c -o test 的输出确实包括:COLLECT_GCC_OPTIONS='-E' '-v' '-o' 'test' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' '-mtls-dialect=gnu' 这确实表明 -E 可能是一个问题
  • 好了。它链接到预处理器。这就是-E 本质上的意思——转储预处理的输出。从这里我认为 Stackoverflow 的工作已经完成。您必须自己弄清楚为什么以如此奇怪的方式设置您的安装。

标签: c gcc debian


【解决方案1】:

详细的编译器输出揭示了秘密:

COLLECT_GCC_OPTIONS='-E' [...]

-E 触发 GCC 只是为了预处理输入,因此生成的“测试”文件实际上是具有单个包含解析(递归)的输入文件。

由于您没有显式添加 -E 参数,gcc 显然是隐式添加的。根据 gcc.gnu.org 上的this 讨论,您的 gcc 版本似乎已经编译了这个,所以我假设您必须从固定源代码重新编译 gcc - 或者从任何来源(Debian)获取正确编译的 gcc 版本存储库?)。

【讨论】:

    【解决方案2】:

    您可以尝试使用\gcc 而不是gcc。前导反斜杠告诉 shell 忽略任何可能影响同名可执行文件的别名。你也可以使用完整的 gcc 路径来达到同样的效果。

    您还可以使用type gcc、whatis gcc 或which gcc 甚至使用较低级别的set|grep gcc 来检查是否存在名为gcc 的别名或函数。

    【讨论】:

    • 欢迎来到 Stack Overflow。如果您通过 cmets 进行操作,您会发现问题已解决 — 在 Stack Overflow 可以解决的范围内 — 通过推断命令行中有一个杂散的 -E。虽然我同意如果gcc 的别名包含-E,那么您的技术将避免该问题,但评论中有证据表明问题比这更根本。
    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多