Xcode
Building Unix/Linux Applications with Xcode
These instructions relate to using Xcode version 15.4 (15F31d). 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 > Project
from the menu.Choose an appropriate target, probably:
macOS > Application > Command Line Tool
.Choose
Next
then name your project and select theLanguage
.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 Scheme…
.Select the project folder in the project navigator.
On the
Info
tab, underTARGETS
, delete the default target that has been created.Delete the default source code.
Create a new target with
File > New > Target…
. ChooseExternal Build System
from theOther
section of theOther
tab.Note: Alternatively, use Finder to drag the folder containing the sources to the source folder of the project. Xcode will automatically prompt to create an external build system project. Xcode should not copy the files, but instead add references to the source files in the project. You can remove the references to any irrelevant files, e.g. the Makefile.
Click
Next
.Enter a product name.
Select the build tool as
/usr/bin/make
.Click
Finish
.Select the newly created target.
On the
Info
tab, select the directory containing the source code's Makefile.Leave
Pass build settings in environment
selected.If necessary, on the Build Settings tab, add a
PATH
variable 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, selectRun Debug
then theInfo
tab and change the value ofExecutable
to the location of the recently built executable by selecting theOther…
option.On the
Arguments
tab 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