【问题标题】:Can't compile code for x86 but can for arm无法为 x86 编译代码,但可以为 arm
【发布时间】:2017-03-20 10:45:34
【问题描述】:

我有一个奇怪的问题。我可以为arm 编译我的代码,但不能为x86 使用gcc v 6.3(以及其他)。 这是导致问题的示例代码。

#include <stdint.h>
#include <cstdio>
#include <unistd.h>
#include <csignal>

volatile bool $run = true;

static void quitHandler(int __attribute__((unused)) signum)
{
    $run = false;
}

void setupQuitHandler()
{
    struct sigaction action;
    action.sa_handler = quitHandler;
    action.sa_flags = SA_RESETHAND;

    if (sigemptyset(&action.sa_mask) < 0) printf("[SetupQuit] sigemptyset");

    if (sigaction(SIGINT, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGINT");
    if (sigaction(SIGQUIT, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGQUIT");
    if (sigaction(SIGTERM, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGTERM");
    if (sigaction(SIGABRT, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGABRT");
}

int main(int argc, char **argv) {

    setupQuitHandler();
    while($run)
    {
        sleep(1);
        printf("Do nothing! \n");
    }
}

这是一个带有 gcc 的online example

这会导致汇编错误:

Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
/tmp/ccEUYGVm.s: Assembler messages:
/tmp/ccEUYGVm.s:19: Error: junk `(%rip)' after expression
/tmp/ccEUYGVm.s:19: Error: unsupported instruction `mov'
/tmp/ccEUYGVm.s:137: Error: junk `(%rip)' after expression
/tmp/ccEUYGVm.s:137: Error: operand type mismatch for `movzb'
make: *** [subdir.mk:26: main.o] Błąd 1

更奇怪的是我可以用clang编译它。这里online example

我已经尝试使用 -O0 -O2-03 参数的 gcc 版本 4.8、5.x、6.3(x- 我不记得了)。

如果我删除 #include &lt;csignals&gt; 也尝试过 (&lt;signal.h&gt;) 并且所有家属都没有问题。但很高兴能捕捉到“INT”信号。

【问题讨论】:

    标签: c++ gcc compiler-errors g++ cross-compiling


    【解决方案1】:

    $ 不是有效的标识符字符。这是一个 gcc 扩展,它可能适用于每个可能的头文件,也可能不适用。不要使用$,错误就会消失。

    【讨论】:

    • 那是扩展程序的错误?疯狂的诊断!
    • @BoundaryImposition gcc 处理得很好,并将结果传递给汇编程序。现在碰巧 $ 对于不同的汇编程序具有不同的含义。我不知道是否有办法逃脱 binutils 会理解的“$”。为了让它在 x86 上“工作”,gcc 还可以修改变量名,但这似乎没那么有用。
    • @n.m.也许你知道为什么没有警告?汇编错误不是很有帮助。 (我只是好奇)。
    • 使用-pedantic,得到各种警告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    相关资源
    最近更新 更多