Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode multiple main methods

I am using Xcode for c programming. I have a project called system_commands. Inside this project I have a file A, and a file B.

This is file A:

int main(int argc, char *argvector[])
{
    char *arguments[] = {"./runProcesses","Hello","World",NULL};
    execvp("./runProcesses", arguments);
    return 0;
}

File A simply "executes" File B, and file B is here:

int main(int argc, char *argvector[])
{

    for(int i = 0; i < argc; i++)
    {
        printf("%s", argvector[i]);
    }
//some forks etc
}

Obviously, Xcode will not allow this, since there are two main methods inside the project. But is there a workaround somehow? Do I really have to create another project, so that the files reside in different projects? Or is there a way I can execvp() a file without main method?

like image 633
p3ob2lem Avatar asked Oct 12 '16 18:10

p3ob2lem


People also ask

How do I add another project as dependency in Xcode?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

How do I create a .cpp file in Xcode?

Start Xcode and choose File / New Project. Click on Other then choose External Build System. Click Next. Input your project name, I use the existing project's name so that the Xcode project file matches.

How do I open two projects in Xcode?

It turns out that the “Open Project…” dialog in Xcode supports multi-select! So you can Shift-Down/Up to select more than one project, hit Return and all three projects will be opened simultaneously!


1 Answers

If you don't want two projects, you can create a second target ("File" - "New" - "Target..." and choose another "Console app") for your second program. Put your other main.c there:

two targets You can also go to the Target settings for the first console app and add a dependency for the second console app, that way, it will build the second, if needed, whenever you run the first one.

dependency

Frankly, this multiple target approach is designed when you have an app with some shared code base, but it can be used in this scenario, too.


The other approach is to go ahead and have multiple projects, but work with them under a single "workspace". So, create a folder, create your two projects in that folder, and then create a "Workspace" (which I called "MyProject", in my example below) and add the two xcodeproj files for the two separate projects ("FirstApp" and "SecondApp" in my example below).

So, in Xcode it looks like:

workspace

And in the file system, it looks like:

filesystem

So, in this case, you have two projects, but you can work with them together under a single workspace, making it really easy to work with both projects at the same time. Just open that xcworkspace workspace rather than the individual xcodeproj files.

like image 54
Rob Avatar answered Oct 14 '22 00:10

Rob