title: How to solve this linker error I get when trying to link C++ with Assembly?
body: <p>I'm trying to link a C++ file and an Assembly file.  The Assembly file defines a C++ function called <code>add</code>.  I'm using a 64 bit Mac OS.  I get the following error when I try to compile the program:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_add", referenced from:
        _main in main-d71cab.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  make: *** [main.o] Error 1
  </code></pre>
  
  <p><b>Makefile</b></p>
  
  <pre><code>hello: main.o hello.o
    g++ main.o hello.o -o hello
  
  hello.o: hello.asm
    nasm -f macho64 hello.asm -o hello.o
  
  main.o: main.cpp
    g++ main.cpp -o main.o   
  </code></pre>
  
  <p><b>hello.asm</b></p>
  
  <pre><code>global add
  
  segment .text
  add:
    mov           rax, rdi
    add           rax, rsi
    ret
  </code></pre>
  
  <p><b>main.cpp</b></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  extern "C" int add(int a, int b);
  
  int main(int argc, char* argv[]) {
  
    int a, b;
  
    std::cout &lt;&lt; "Enter two numbers\n";
    std::cin &gt;&gt; a;
    std::cin &gt;&gt; b;
    std::cout &lt;&lt; "Sum of a and b is " &lt;&lt; add(a, b) &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>I'd really appreciate any help.  Thanks!                 </p>