在Xcode中构建C ++和Assembly源代码 [英] Building C++ and Assembly source in Xcode

查看:255
本文介绍了在Xcode中构建C ++和Assembly源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在OS X 10.9的Xcode中构建一个命令行应用程序,其中包含一个.cpp源文件,该文件使用在.asm程序集文件中外部定义的函数. C ++代码:

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

以下是汇编函数:

global NOTHING

section .text

NOTHING:

    mov eax, 0
    ret

这是一个什么都不做的程序,只是将零值暂时移到EAX寄存器中.创建.asm源文件时,请确保选择NASM程序集.当我点击播放"按钮生成可执行文件时,Xcode只是声明构建失败,而没有指定原因.

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.

我可以像在Linux上一样,恢复到命令行中的所有操作.但是,如果可能的话,我宁愿开始使用Xcode,因为它结合了许多工具,例如将Git集成到一个用于开发的应用程序中.

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.

编辑:回答之后,我决定放弃Xcode;命令行要简单得多.根据答案,我为以后访问该问题的用户编写了以下"makefile"文件:

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

假定汇编文件为"asm.asm",C ++为"main.cpp",并且创建的可执行文件名为"test".如答案中所述,请确保.asm文件中的功能以下划线开头.

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.

推荐答案

您需要为64位x86-64 Mach-O对象文件指定-f macho64-.如您所见,Mach-O(功能)符号以下划线为前缀.因此,如果给函数定义NOTHING,则必须在程序集中提供全局_NOTHING.

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.

此外,具有"C"链接的函数应指定为:void NOTHING (void);

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

这篇关于在Xcode中构建C ++和Assembly源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆