Search code examples
cassemblygcc

error: expected identifier or ‘(’ before string constant


I was trying to compile the code at the bottom of https://www.cs.uaf.edu/courses/cs301/2014-fall/notes/inline-assembly/ with gcc and got an error.

First, here's the code:

extern "C" long my_fn(long in); /* Prototype */

__asm__( /* Assembly function body */
"my_fn:\n"
"  mov %rdi,%rax\n"
"  add $100,%rax\n"
"  ret\n"
);

int foo(void) {
   return my_fn(3);
}

Here's the error:

test.c:1:8: error: expected identifier or ‘(’ before string constant
    1 | extern "C" long my_fn(long in); /* Prototype */
      |        ^~~
test.c: In function ‘foo’:
test.c:11:11: warning: implicit declaration of function ‘my_fn’ [-Wimplicit-function-declaration]
   11 |    return my_fn(3);
      |           ^~~~~

Any ideas?


Solution

  • It looks like you are trying to compile C++ code using a C compiler.

    Either compile with g++ (the C++ version of gcc), or rewrite the code to be valid C by removing the problematic extern "C" from the line in question.

    long my_fn(long in); /* Prototype */