2

I'm trying to build a command-line application in Xcode for OS X 10.9 which contains a .cpp source file, which uses a function externally defined in a .asm assembly file. The C++ code:

#include <iostream>

using namespace std;

extern "C" void NOTHING();

int main(){

    NOTHING();

    return 0;    
}

The following is the assembly function:

global NOTHING

section .text

NOTHING:

    mov eax, 0
    ret

It's a program that does nothing but temporarily move the value zero into the EAX register. I made sure to choose NASM assembly when creating the .asm source file. When I hit the 'play' button to build the executable, Xcode simply states build failed, without specifying a reason.


I could revert back to doing it all in the command line, as I would on Linux. However, if possible, I'd prefer to start using Xcode, as it combines many tools, e.g. Git, into a single application for development.


EDIT: After the answer, I have decided to abandon Xcode; the command line is just much simpler. Based on the answer, I have written the following 'makefile' for future users visiting the question:

test: main.cpp asm.o
    g++ -stdlib=libstdc++ main.cpp asm.o -o test

asm.o: asm.asm
    nasm -f macho64 asm.asm -o asm.o

which assumes the assembly file is 'asm.asm', the C++ 'main.cpp', and the executable created is named 'test.' As in the answer, make sure functions in the .asm file begin with an underscore.

user45389
  • 81
  • 8
  • You can see the reason for each file in the organizer (on the left side of Xcode). – The Paramagnetic Croissant Jul 08 '14 at 17:43
  • @user3477950: It appears there's a problem with the linker; says symbols not found for architecture x86_64. – user45389 Jul 08 '14 at 18:11
  • @userXXX And which symbols aren't found? Perhaps you just need to link against the C++ standard library. – The Paramagnetic Croissant Jul 08 '14 at 18:12
  • @user3477950: "Undefined symbols for architecture x86_64: "_NOTHING", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" I read that you had to use _NOTHING rather than NOTHING in the .asm code. I did that, but still got the error. – user45389 Jul 08 '14 at 18:29

1 Answers1

3

You need to specify -f macho64 - for 64 bit x86-64 Mach-O object files. As you've already seen, Mach-O (function) symbols are prefixed with an underscore. So if you give the function definition NOTHING, you must provide the global _NOTHING in the assembly.

Also, a function with "C" linkage should be specified as: void NOTHING (void);

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • +1 until the last sentence. We aren't in the K&R C days anymore, personal style preferences should be left out imho. – JustSid Jul 08 '14 at 18:40
  • Abandoned Xcode, and did everything in the command line; your suggestions worked. – user45389 Jul 08 '14 at 18:44
  • @JustSid - It is still better practice, since it's function with C linkage. Putting the same declaration in C source will silently accept any arguments. – Brett Hale Jul 08 '14 at 18:53