简体   繁体   中英

referencing global variables from inline assembly in gcc

I have the problem where the linker generates undefined reference errors from the inline assembly code.

int global_var = 0;
void myfunc()
{
    asm(".intel_syntax noprefix\n");
    asm("lea eax, global_var\n");
}

I am compiling with -masm=intel and with no optimizations or anything, using GCC 3.4.2 If anyone suffered from this inconvenience too please assist.

Basically, it's an issue of name mangling - that is to say, the compiler mangles(alters) the names of variables and functions during the compile phase. In this instance, "global_var" is altered to "_global_var"

If you change the 2nd line of your function such that it accesses "_global_var" then it compiles just fine. (tested GCC 4.7.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM