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.

  1. After launching Xcode, choose the option to create a new project, or select File > New > Project from the menu.

  2. Choose an appropriate target, probably: macOS > Application > Command Line Tool.

  3. Choose Next then name your project and select the Language.

  4. 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.

  5. The project is created with a default target and probably a default source code file.

  6. It may also have created a scheme. If so, delete the scheme under Product > Scheme > Manage Scheme….

  7. Select the project folder in the project navigator.

  8. On the Info tab, under TARGETS, delete the default target that has been created.

  9. Delete the default source code.

  10. Create a new target with File > New > Target…. Choose External Build System from the Other section of the Other 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.

  11. Click Next.

  12. Enter a product name.

  13. Select the build tool as /usr/bin/make.

  14. Click Finish.

  15. Select the newly created target.

  16. On the Info tab, select the directory containing the source code's Makefile.

  17. Leave Pass build settings in environment selected.

  18. 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.

  19. Once you have built the application with Product > Build, edit the scheme, select Run Debug then the Info tab and change the value of Executable to the location of the recently built executable by selecting the Other… option.

  20. On the Arguments tab of the scheme editor, add any arguments the application needs on launch.

  21. 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.

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