简体   繁体   中英

Building C++ and Assembly source in Xcode

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, eg 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.

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);

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