0

I'm trying to compile the code referred to in the below link. I'm getting the below linker error:

/tmp/ccUVLIZ0.ltrans0.ltrans.o: In function `main':

:(.text.startup+0x5): undefined reference to `one'

collect2: error: ld returned 1 exit status

Equivalent for GCC's naked attribute

The linker is not seeing the assembly definition?

Code below:

#include <stdio.h>

asm("_one:              \n\
    movl $1,%eax    \n\
    ret             \n\
");

int one();

int main() {
    printf("result: %d\n", one());
    return 0;
}
  • 2
    On some platforms functions aren't prefixed with an underscore. – Some programmer dude Aug 28 '18 at 16:19
  • Is this on MacOS? – Michael Petch Aug 28 '18 at 17:10
  • on linux and Some programmer dude was right.. – SubliminalBroccoli Aug 28 '18 at 17:11
  • I asked because MacOS using Mach-O requires an underscore. From the output I could tell it wasn't on Windows. If you are using C++ (and not C) you'd have to follow the advice in Ivento's post because of C++ name mangling. Is this being compiled as C++ or C? I know you have tagged this c++ but I'm unsure that is an error or not? – Michael Petch Aug 28 '18 at 17:13
  • The other question you link to was for FreeBSD (not Linux)so that is why there was an underscore. The other question seems to be for C and not C++. – Michael Petch Aug 28 '18 at 17:17
  • @MichaelPetch sorry yes it's for c++ on linux, and Ivento's advice does work.. I actually have another question, but will create another thread/question for it..thank you.. – SubliminalBroccoli Aug 28 '18 at 17:40
  • @SubliminalBroccoli : If Ilvento's answer provided you what you need (a solution that works) would you consider accepting it as an answer. More on HOW and why you want to accept an answer can be found here: https://meta.stackexchange.com/a/5235/271768 – Michael Petch Aug 28 '18 at 17:47

1 Answers1

1

For such tricks, you need to explicitly provide function specification

#include <stdio.h>

asm("one:              \n\
    movl $1,%eax    \n\
    ret             \n\
");

extern "C" int one();

int main() {
    printf("result: %d\n", one());
    return 0;
}

You probably can find more explanations about extern "C" in What is the effect of extern "C" in C++?

Askold Ilvento
  • 1,405
  • 1
  • 17
  • 20