S.R
S.R

Reputation: 2751

Can't compile code for x86 but can for arm

I have a strange problem. I can compile my code for arm but can't for x86 using gcc v 6.3 (and other also). Here is an example code that causes problems.

#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");
    }
}

Here is an online example with gcc

This cause assemble error:

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

What is more strange I can compile it with clang. Here online example

I have tried gcc versions 4.8, 5.x, 6.3 (x- I don't remember) with -O0 -O2 and -O3 parameters.

If I remove #include <csignals> also tried (<signal.h>) and all dependents there is no problem. But it's nice to catch 'INT' signal.

Upvotes: 0

Views: 368

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120109

$ is not a valid identifier character. It's a gcc extension which may or may not work with every possible header file. Don't use $ and the error goes away.

Upvotes: 5

Related Questions