如何使用可执行文件将C ++代码合并到Xcode项目中? [英] How can I incorporate my C++ code into my Xcode project using an Executable file?

查看:110
本文介绍了如何使用可执行文件将C ++代码合并到Xcode项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xcode的新手,我只是想看看是否有可能在Xcode中使用可执行文件.为了测试这一点,我有一个非常简单的C ++代码,它将用户的输入输出到屏幕上.

I am new to Xcode, and I simply want to see if it is possible to use an executable file in Xcode. To test this, I have a very simple C++ code that prints the user's input to the screen.

我正在Xcode 10中处理基于swift的项目.在项目构建设置中,我可以看到链接部分,该部分似乎是放置可执行文件的适当位置,但是我发现了所有信息.关于从Xcode项目生成可执行文件,这与我需要的相反(将外部可执行文件合并到我的Xcode项目中).这是我从中生成可执行文件的C ++文件中的主要代码.

I am working with a swift-based project in Xcode 10. In the project build settings, I can see the linking section which seems like the appropriate place for a executable file to be placed, however all of the info I have found has been about generating an executable file from the Xcode project, which is the opposite of what I need (to incorporate an external executable file into my Xcode project). Here is the main code from the C++ file from which I generated the executable file.

int CPPTest::main(int argc, char* argv[])
{
int counter;
//  printf("Program Name Is: %s", argv[0]);
if (argc == 1)
printf("\nNo Extra Command Line Argument Passed Other Than Program Name\n");
if (argc >= 2)
{
    printf("\nNumber Of Arguments Passed: %d", argc);
    printf("\n----Following Are The Command Line Arguments Passed----\n");
    for (counter = 0; counter<argc; counter++)
    printf("argv[%d]: %s\n", counter, argv[counter]);
}
//  system("pause");
return 0;
}

对于如何将可执行文件与Xcode项目链接以及如何调用主函数并传递参数的逐步说明,我将非常感激.

I would greatly appreciate step-by-step instructions as to how to link my executable file with my Xcode project, and to be able to call the main function and pass the parameters.

推荐答案

如果是iOS项目,则将无法直接运行C ++可执行文件.您可以通过将一个目标添加到项目并将该库链接到您的Swift应用程序来为C ++代码创建一个库.以下堆栈溢出问题应有帮助:

If it's an iOS project, you won't be able to run the C++ executable file directly. You could create a library for the C++ code by adding a target to your project and link the library with your Swift app. The following Stack Overflow question should help:

>如何构建和使用C ++ ios应用程序的静态库

如果是Mac项目,则可以使用Process(以前称为NSTask)类运行C ++可执行文件并提供参数.您必须将可执行文件添加到项目中,将复制文件构建阶段添加到Swift应用程序项目中,才能将C ++可执行文件复制到应用程序包中,并编写一些Swift代码以从应用程序包中加载C ++可执行文件.

If it's a Mac project, you can use the Process (formerly called NSTask) class to run the C++ executable and supply the parameters. You have to add the executable file to your project, add a Copy Files build phase to your Swift app project to copy the C++ executable to your app bundle, and write some Swift code to load the C++ executable from the app bundle.

这篇关于如何使用可执行文件将C ++代码合并到Xcode项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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