简体   繁体   中英

How can I incorporate my C++ code into my Xcode project using an Executable file?

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.

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

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.

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:

How to build and use a C++ static library for ios application

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.

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