Xcode
Building Unix/Linux Applications with Xcode
These instructions relate to using Xcode version 16.3 (16E140). It describes how you can use Xcode as an IDE for Unix/Linux C/C++ projects that have a Makefile.
Xcode can also be used for integrated debugging, but the code must be compiled
with debug symbols otherwise Xcode debugging appears not to work, with no
error messages or other help.  Debugging is often easier if optimisations have
also been disabled, by passing -O0 (capital letter O followed by zero) to
the linker.  E.g. typically for C++, passing the -g (Generate source-level
debug information) and -O0 to Clang g++ is achieved by setting CXXFLAGS
when running GNU ./configure, e.g.
./configure CXXFLAGS='-g -O0'
It may be easier to use the LLDB debugger (which is used by Xcode) from the command line to troubleshoot any issues with debugging. Once it works from the command line, it should work within Xcode. Some notes for quickly getting started with LLDB are in another section below.
- After launching Xcode, choose the option to create a new project, or select - File > New > Projectfrom the menu.
- Choose an appropriate target, probably: - macOS > Application > Command Line Tool.
- Choose - Nextthen name your project and select the- Language.
- Select the folder under which the new project will be created with a folder name largely matching the provided project name and created the project. 
- The project is created with a default target and probably a default source code file. 
- It may also have created a scheme. If so, delete the scheme under - Product > Scheme > Manage Schemes….
- Select the top-level project folder in the project navigator. 
- Delete the default target that has been created under - TARGETS.
- Delete the default source code. 
- Use Finder to drag the folder containing the sources to the source folder of the project. Xcode will automatically prompt to - Choose options for adding these files. Choose an- Actionof- Reference files in placeand chose- Groupsas- Create groups. You can remove the references to any irrelevant files, e.g. the Makefile.
- Create a new target with - File > New > Target…. Choose- External Build Systemfrom the- Othersection of the- Othertab.
- Click - Next.
- Enter a product name. 
- Select the build tool as - /usr/bin/make.
- Click - Finish.
- Select the newly created target. 
- On the - Infotab, select the directory containing the source code's Makefile.
- Leave - Pass build settings in environmentselected.
- If necessary, on the Build Settings tab, add a - PATHvariable to include any binaries required to complete the build, e.g.- PATH :/opt/local/bin- Note: This value is appended to system's path without a path separator, so it needs to start with a path separator. The path can be set for the entire project or just the target. 
- Once you have built the application with - Product > Build, edit the scheme, select- Run Debugthen the- Infotab and change the value of- Executableto the location of the recently built executable by selecting the- Other…option.
- On the - Argumentstab of the scheme editor, add any arguments the application needs on launch.
- Run the application from Xcode menu with - Product > Run.
The Product > Clean Build Folder… option does not run the clean target of
the Makefile.  It seems you have to duplicate the target and specify on the
Info tab of the new target an argument of clean replacing the $(ACTION)
variable.  Then create a new scheme (Product > Scheme > New Scheme…) setting
it's target as the newly created target.  Although one would have thought
there was a way of passing different targets to the make file via the ACTION
variable, it eluded me.
- gcc - using a Makefile in Xcode - or alternative IDE - Stack Overflow
- Porting UNIX/Linux Applications to OS X
Debugging from the Command Line with LLDB
Launching the debugger
$ lldb $TARGET -- $ARGS
where $TARGET is the executable to be debugged and optionally, $ARGS are
arguments to pass to the executable.
alternatively:
$ lldb
(lldb) file $TARGET
(lldb) process launch -- $ARGS
Commands Quick Reference
(lldb) breakpoint set --name main
(lldb) breakpoint set --file main.cpp --line 42
(lldb) b main.cpp:42
(lldb) breakpoint list
(lldb) br l
(lldb) breakpoint delete 1
(lldb) br del 1
(lldb) display foo
(lldb) undisplay foo
(lldb) help source list
(lldb) help bt
(lldb) bt
Troubleshooting
Resources
-- Frank Dean - 26 May 2024
Related Topics: InstallingMacPorts, MacOSXOpenJDK, MacOSXTips, iOSDevelopment, watchOSDevelopment