Xcode: multiple projects, more than one main executable - c++

Hi I'm new to Xcode. I'm trying to learn c++ and I was wondering if it is possible to have multi projects and choose which project should run. I used ms visual studios before and I like how under a solution I can create multiple projects and choose which one is the executable one. Xcode also has targets not exactly sure what they are maybe they play a role? I tried messing around but I constantly have issue because of duplicate mains. I'm going through a chapter and there are case studies. I'm trying to keep every chapter organized in one project well something like that. Any help thanks!

Xcode targets are probably what you are looking for. You can have many targets in a single project, and whichever one you select as active will be used when you tell Xcode to compile or run. If you are having problems with duplicate mains, you probably have all of your .cpp files in all of your targets. If you have main1.cpp and main2.cpp in the same target, Xcode will attempt to use both of them and run into a conflict. Including all common files in all targets, but only the appropriate main files will likely solve your problems.

for creating a new main.cpp file you should go to
File-New-target.
After it gets created just select your file here to choose which file you want to execute:

Actually you can create a workspace, then create each exercise as a project or as a target inside a root project, the second option is preferred if the exercises need to share the same libs and setup.
To select the active scheme to debug and run you can use this selection menu close to debug controls.

Related

How do i use a bunch of .h and .cpp files to create a static library that can be used in another c++ Project

I am trying to learn C++ from some Stanford Course and they have a custom library which contains a bunch of header files(.h) and their associated source (.cpp) files.
I want to create new C++ projects in xcode 6.x to try out code and include those custom header files in my new projects.
I have no freaking idea how to include those files in my project so that i can peacefully #include "blahblahblah.h" and use functions from that header file without issues.
I am new to both xcode, c++(although i know some coding part) and anything else with this process.
Summary:
I have a folder Library which contains a series of .h and .cpp
files.
I have XCode 6.x.x/Eclipse installed.
I want to create a new C++ project and use #include
"OneOfThoseHeaderFiles.h" and then use a function or something from
those header files.
Also assume i have no clue how to run some command line codes that i see a lot of.
If someone can please tell me in the most simple way(preferably step-by-step) i would be so eternally grateful and sing praises to you until the day i die :)
Note: Either XCode or Eclipse is fine. I really just want to start coding.
I have spent the better part of a whole week trying out xcode, eclipse etc and trying to follow a ton of steps to get it working but i just cant get how to add the files without issues. Either the info seems to be for Xcode 4.x and every menu seems changed or the suggestions are so complicated that a novice like me cant figure it out.
First off, almost all the information for Xcode will be available on the Apple Developer's portal:
https://developer.apple.com/library/prerelease/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/start_project.html#//apple_ref/doc/uid/TP40010215-CH2-SW5
To create a simple static library on Xcode 6 you start by creating a new project, then under OS X - Framework and Library select Library, on the next window change the Framework to None (Plain C/C++ Library) and static (since that is what your are stating).
Once your project is created, add the existing headers and sources to your project, either by dragging them with the mouse or with the add files drop menu from the right click functionality, you can create a new group in Xcode if you want some order for them.
When adding the files, on the add window shown you can select to either copy them or use their current location.
If needed you might have to change the "header search path" in your target configuration under "Build Settings", if it is the first time you are using Xcode some of this will sound weird but I am sure you will find most of it on the Apple Developers portal in great detail.
If you want to test the files without having to create an external static library you could create a command line project and test them faster and easier.

Quickly create and run new C++ files in XCode

I'm learning c++ and I'm building simple, procedural programs that only consist of the one file (where int main() is located). I find it fairly tedious to create a new project in Xcode for each file and I was wondering whether there's a way to either have multiple files in a project and choose which one to compile, or a faster way to create and run/build C++ files in Xcode. I'm currently just commenting programs out as I go and I'd appreciate someone suggesting a better way. I'm open to using Sublime and Command line as well
You can choose which files to build and run by changing the Scheme.
I am assuming that you are creating a Console Application / target for each set of files you want to run separately.
go to Product->Scheme and change the Scheme.
Then Build and Run your files.
I think command line is the way to go here. You can easily edit the files in XCode (open -a Xcode file.cpp) and compile and run it from the terminal (clang++ -std=c++11 -stdlib=libc++ -o name_of_executable file.cpp).
An alternative to having multiple targets in XCode is to have one command line target and change the file that you compile as a part of the target. In XCode 5 you can show the Utilities panel (top right icon - Hide or show the utilities) and check/uncheck whether a selected file belongs into a target (in the window titled Target Membership). That way you can have a number of files, each having its main() but you always have only one file selected as a part of the target.
You can create a single project file, then create a new target for each new app instead of a new Xcode project. I'm not sure if that meets your needs or not.
Although if all you're doing is compiling and running a single file, why even use Xcode? Just go to the command line and compile and run it directly, if that's easier. You can invoke clang directly. See the man page for details.

Multiple .cpp programs in xcode project?

I'm using Xcode to do the Project Euler problems in C++. I wanted to keep everything organized, so I decided to make a single project that would include all my .cpp files, like in visual studio. The problem that I am encountering is I have one project called "Project Euler" and then I wanted to have Problem1.ccp, Problem2.cpp, etc. enabling the ones I want to run. When I did that, I got an error "Apple Mach-O Linker Error". Does anyone know how I could do what I'm trying to do with Xcode?
What you want is one Xcode target per problem (assuming that each problem has a main()).
To add a target to the current project (Xc4), go to File > New > New Target….
Yes, you want to add a new Target using justin's steps.
You also need to understand what a target is: http://developer.apple.com/library/ios/#featuredarticles/XcodeConcepts/Concept-Targets.html
and how to run and compile the separate files:
http://developer.apple.com/library/ios/#recipes/xcode_help-scheme_editor/Articles/SchemeBuild.html
To paraphrase, a target is a completely separate product contained in the same Xcode project.
When you want to change which target you're running/compiling/analyzing you need to adjust the scheme. In Xcode version 4.6.2 go to:
Product > Scheme > Desired Target

Visual Studio: how to create a project that would compile 2 exe files?

So I have main.cpp and main2.cpp with int main in each. I want to get 2 exes out of it. Is it possible and what would be instruction to create such project?
Nope, Visual Studio's project model is rigidly built around the assumption that "one project generates one output".
If you need two executables, you have to create two projects. You can keep them in the same solution to make things easier for yourself, but they have to be separate projects.
Edit
Ok, as other answers have pointed out, it can of course be done, if you're desperate. You can add a custom build step, which does anything you like, including building another executable. (However, the build system won't understand that this file should be considered project output, and so some scenarios may fail: for example the file won't be automatically copied to the output folder, and when checking dependencies before a rebuild, it might not be able to understand which files to check, and what (or how) to rebuild.)
Visual Studio (at least up to 2008, not sure about 2010) also allows the use of nmake files, but then I think you're stretching the definition of "a Visual Studio project".
But under "normal" circumstances, one project implies one output. And in order to get two executables, you'd normally create two projects.
You need a solution which includes two projects. Have a read of the Visual Studio documentation on solutions and projects.
Here's my solution, since nobody in a Google search seems to suggest this method. It's quite simple and I've used/seen it used in other IDEs (like Code::Blocks).
Within your project, create a configuration for each output that you want. Then, only include one main source file in each configuration.
In VS, this means for each source file with main: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one main source is built for each configuration. You can then specify a different output for each configuration in the Project Properties. I did this on VS 2010, but it should probably work with other versions.
I'm using this approach so that I can have several tests for one project, without cluttering the solution with more test projects than actual code projects.
I don't know if it can be done ,but the only change you have ,to do this ,is with custom build step.
EDIT: Since someone downvoted this ,i did a test making a dummy configuration.
In the custom build step I two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.obj resp. main2.obj and outputting App1.exe resp. App2.exe.
It's executed after Compiling and before linking.
It worked !
The downside is I cannot prevent (yet) the linking ot the orinal exe (which errors on duplicate main function).
Solution to this could be to have a lib project excluding the sources with main()from build and build them in the custum-step too.
So the answer to the question should : Yes ,it can be done!
You can't have more than one main() function in a single visual studio project. So you can't compile 2 executables, the only way is to make two different project in the same solution
You can create a solution with two project one for each output you want. Then head to Build menu and select Batch Build.. and select both projects.
If you want both exe files to be in one place you can specify a custom Post-build action:
For both project:
view the project properties page and in Build events select Post-Build events, then in the Command line field enter the command that will copy the output to the location you want, something like:
copy $(TargetPath) c:\yourlocation /Y
Then after you build the solution the output files will be copied to that location.
Another option you have is to use conditional compilation with sth like
main()
{
#ifdef VERSION1
call_main_logic();
#else
call_main2_logic();
#endif
}
and then define different configurations within the project. For each configuration you will then need to define preprocessor symbols appropriately (in: project settings -> configuration properties -> C/C++ -> preprocessor).
Please note, that it will be only one executable created at a time, but with switching configurations you'll get the one that does what you want at the moment.
... which may suit your needs or not, depending on more specific situation that you are in.
Edit: In addition, since different configurations create their separate output folders, you will have your both execs as outputs.

Import existing C++ project into Xcode IDE

I am trying to open an existing C++ open-source library in Xcode to publish it with my own modification/additions. The library is Tesseract-OCR, which does not include a .xcodeproj file.
Since Xcode can function as an IDE, is it possible to open a bunch of files as a single project in Xcode? Is there an easy way to produce an Xcode project?
There are several ways you could do it, depending on the level of IDE integration you want. There's no direct way of importing a Makefile-based project into Xcode. You can create a project that builds via the Makefile, but you wouldn't get many of the benefits of using an IDE, since the editor features such as word completion rely on Xcode being able to parse the files in the project. You will be able to use the debugger though. To do this, create a new project and add a custom target with a script build phase that just calls down to Makefile.
If however the project you're building compiles very easily, ie without requiring a lot of macros to be set up, include paths, etc, then it may be simple to just create an empty project and merely add all source files to it. I've used this method extensively for building boost libraries. If this is a configure && make type project then you will probably have to run the configure step first, and ensure any top level config.h files are included in the project.
If the project has a complex makefile then it is likely to be an involved task to create a useful Xcode project
I realise you asked explicitly for Xcode, but in case you were actually trying to solve the problem of "I have existing C++ code which builds and runs fine from the command line, and I'd like to code and debug it in an IDE, what should I do?" my firm recommendation would be to avoid Xcode and go for Eclipse.
The reason is that as far as I can tell, Xcode has no way of ingesting the command line build environment and effectively requires you to recreate the make process inside Xcode from scratch. Fine for tiny projects, but anything with more than a few source files and it quickly becomes painful. Whereas in Eclipse everything is built around Makefiles. So in my case I got to the "step through code with working code completion" in Eclipse a lot quicker vs. never in Xcode. This of course could be because I'm an Xcode noob, but my 2c.
To create an Xcode project from an existing cmake project, you can run cmake -G Xcode. It produces some folders and files apart from the project file, so it might be better to create a folder for it first. For example:
mkdir -p build/xcode
cd build/xcode
cmake -G Xcode ../..
Xcode is a useable IDE for library creation.
Of course a good first step is to see if the one source code will build on its own with configure scripts that are included.
If not, it becomes a question of how many libraries you need to link in.
There are resources online (or at least there used to be) for using Xcode (or perhaps it's forerunner Product builder) for porting Unix projects to Mac.
Good tutorial at: http://www.macresearch.org/tutorial-introducing-xcode-30-organizer
Another good reference is Darwin Ports.
As for doing this on your own. You can build c++ based libraries in XCode. People do that every day. You can even use one of the Xcode templates to get you started.
However, library dev requires more experience with Xcode then say a simple Cocoa "Hello World" app.
The remaining questions will be assuring that the source code's dependencies are already built into the Mac's SDK. (Don't hold your breath for linking to MFC)
It's a general question... So it's a general answer.
In Xcode8,there is "Xcode->file->add files to...",then choose your files.If you want to add several files at a time,press "Cmd" when you are choosing.