Quickly create and run new C++ files in XCode - c++

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.

Related

Clion - Can I debug across projects?

I have the following setup for my C++ application:
Project "Common" with 6 cpp source files. One of these cpp sources is named dataclasses.cpp. The output of this project is libgeneral.dylib, to be used in the next project below.
Project "Algorithm" with only one cpp source file. This project uses the libgeneral.dylib from above and outputs another dynamic library named libalgorithm.dylib, to be used in the project below.
Finally, project "CLI" has one cpp file source file and uses libalgorithm.dylib created above. It creates the executable cli.
(I created this structure because I plan on using parts of the code base in different applications in the future. In my mind, this structure keeps things modular and clean.)
I am able to compile/build everything and run the executable with a 0 return code. However, there is a computational error in the output triggered by running cli against certain data. I suspect the error is in dataclasses.cpp. So my plan was to open the file dataclasses.cpp in project CLI and mark a breakpoint at the right spot, and then run debug in CLI. But when I attempt that, the breakpoint appears as an empty white circle with a diagonal line and a message
The breakpoint will not currently be hit. No executable code is associated with this
line.
Yet this is not true, the line I have chosen is definitely an executable line.
Note that I am able to successfully use breakpoints in the one source file that is used in project CLI.
CLion version: 2022.2
MacOS 12.2.1
Question: How can I debug dataclasses.cpp while running in project CLI? I would prefer to not add any new files in project Common; I want to keep this as a tightly defined library. Note that before the data reaches the relevant part of dataclasses.cpp, it has gone through multiple manipulations in the code within projects Algorithm and CLI. I know that ideally I should write test cases for everything in dataclasses.cpp, but I would like to solve this known issue quickly first.
A similar question was raised here, Can I debug libraries (DLLs) directly using CLion?, and the answer suggesting creating an executable in the library project, which I'd like to avoid. Plus the post is six years old and I'm hoping things have changed.

How do I turn my C++ code into a .app file? [duplicate]

I am building a C++ OpenGL card game in Xcode 4.3.3, with the intention of running it on both 64-bit and 32-bit Macs, which is not my problem. My problem is that I have reached a point in the development of the program where I would like to test that it works on a different 32-bit Mac (mine is 64-bit) without having to install Xcode. This, obviously, requires me exporting it as an .app. I can find and run the exported Unix Executable File from my project, after some cd'ing, through Terminal, but that is undesirable for me and the intended audience of the program. I have trawled the google and Stack Overflow, looking for anything to help me, but all the things mentioning Archiving seem to have been unsuccessful with my particular project, and I think that's because it's a C++ command line tool project. So, can someone please help me export my project as a .app?
Thanks in advance.
The simplest way of doing this would be to create a Cocoa project, go to 'build phases' and remove all the objective-c frameworks from the 'link with libraries' build phase, remove any objective-c files added by the template (app delegate, etc.) and then replace main.c with your main.cpp file.
There's nothing really special about a Cocoa project except for the fact that it links against Cocoa and calls NSApplicationMain() from its main() function.
There are also a bunch of .plist entries used by Cocoa which you'll no longer need. But they won't affect the way OS X treats your application.
I'm searching for an XCode Settings solution but here's my current way to create an app:
You have to take your binary file (let say myapp) and put it in "MyApp.app/Contents/MacOS/myapp" then it can now be launched as a .app. That's a trick.
Just go to the targets folder in the file navigator on the left, and there will be an executable listed there. Left click on that executable and click "show in finder". (I'm describing from memory so I may have the exact details wrong.) The file path is ridiculously long (there is an arbitrary alphanumerical pathname involved, I have no idea why), so you probably won't be able to find it with the command line.
If you want you can change the extension of the file from nothing to ".app", it shouldn't affect the nature of the file at all. Also if you want a custom icon, you can "get info" on it in the finder, then click on the icon in the info window, and use Cmd-V to paste it in (obviously you have to have your icon copied to the clipboard). There is also a way to do this inside of Xcode, but I don't remember it.

How to compile WindRiver/Eclipse C++ projects from the command line?

I'm currently running a variant of Eclipse known as WindRiver, which is designed for embedded systems programming using C++ (specifically, I'm part of my high school's FIRST Robotics team).
I'm able to successfully compile and build the project from within Eclipse (Project > Build Project) but I'm looking for ways to automate this process by compiling by using the command line.
The project already contains a makefile and everything, so ideally I want to be able to just run that without making any manual changes. I pastebin'd the makefile in case its relevant.
Does anybody know where I can find more information on compiling C++ programs from the command line for either Eclipse or WindRiver or on running makefiles on Windows? I tried looking at "How to run a makefile in Windows?" but following the first answer didn't work (it gave a syntax error for the makefile).
I'm currently using a Windows 8 laptop. As best as I can tell, the current varient of WindRiver I'm using is based on Eclipse version 3.3.1.
You will need a make utility, I believe your WindRiver / Eclipse setup would come with "gnumake" (probably called either make or gmake). You'd nee dto set up the command line path to lead to the compiler and the make executable. Unfortunately, this is not a great answer, as I can't give you exact links to the make and compiler locations (it would of course also depend on where you installed things). I just thought I'd lead you somewhat on the right path, since the question has been up for a little while and no one jumped at it.
Thanks to Mat Petersson's answer, I was able to identify everything I needed and create a batch file that could compile the file for me:
#echo off
setlocal
set PATH=%PATH%;C:\WindRiver\gnu\3.4.4-vxworks-6.3\x86-win32\bin;C:\WindRiver\utilities-1.0\x86-win32\bin;C:\WindRiver\setup\x86-win32\bin
set WIND_BASE=C:\WindRiver\vxworks-6.3
cd My_Project\PPC603gnu
make --no-print-directory BUILD_SPEC=PPC603gnu DEBUG_MODE=1 TRACE=1

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.

Xcode: multiple projects, more than one main executable

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.