jms547
jms547

Reputation: 283

using a Makefile in Xcode - or alternative IDE

I am new to Xcode, Makefiles, and to programming generally.

I have been given an existing C++ code to work with that is built using a Makefile. Using the Makefile I can build and run the executable on my Mac (MacOS 10.14.6) with no problem.

The start of the Makefile is

CC = g++-mp-9 -Wall -O2 -std=c++14 -fopenmp
DEBUG = 
CFLAGS = -c $(DEBUG) -I/opt/local/include
LFLAGS = $(DEBUG) -L/opt/local/lib -framework Accelerate -march=native -lfftw3 -lfftw3_threads -lopenblas -lm -lpthread -larmadillo

myproj: $(OBJS)
    $(CC) -o myproj $(OBJS) $(LFLAGS)

main.o: main.cpp const.h functions.h
    $(CC) $(CFLAGS) main.cpp
...

where myproj is the executable produced.

I want to use the debug features of Xcode 11 (or any modern IDE - see ALTERNATIVELY below) to explore and learn the code I want to use. Following these instructions I have created a command-line Xcode project and set up an external build target.

However when I try to build it in Xcode, I get the following error

g++-mp-9 -Wall -O2 -std=c++14 -fopenmp -c  -I/opt/local/include main.cpp
make: g++-mp-9: No such file or directory
make: *** [main.o] Error 1

This is strange because my Makefile works just fine, and on the command line my system can find the compiler:

>$ whereis gcc  
/usr/bin/gcc

Could someone please explain why Xcode can't find the compiler, and how I might fix it? (Also any tips/warnings about using Xcode to grok a code that is designed to be built using a Makefile would be appreciated!)

ALTERNATIVELY
I'm not wedded to the idea of using Xcode, I just thought it would be natural as I'm working on a mac. If there is another IDE with a good debugger that would be more suited to building a command-line executable using a Makefile then please let me know!

Upvotes: 0

Views: 6377

Answers (1)

jms547
jms547

Reputation: 283

Thanks to the help of MadScientist I figured out the problem that the PATH environment variable wasn't being imported from bash (see also here). Through trial and error I worked out that I needed to set the right PATH a few places in Build Settings, and tick the Pass build settings in environment checkbox when creating the new external-build target.

For future reference, this is the full procedure I used. Recall that my problem was that I wanted to
- Import a gnu Makefile project into Xcode 11,
- The project was a set of source files and a Makefile that produced a program to run on the command line,
- The source files (and hence Makefile) depend on libraries found in a PATH that I need to set in Xcode (in my case I had installed the libraries via MacPorts).

  1. File > New > New Project.
  2. In the dialogue box select macOS > Command Line Tool, Product Name etc. and language. Enter your location for the project.
  3. Delete the default target and the two default folders.
  4. File > New > Target... to create a new target, and then Cross-platform > External Build System. Select a (Product) Name for the new target, and the Build Tool (mine was /usr/bin/make).
  5. (with the new target from step 4 selected in the editor area) In the Info > External Build Tool Configuration tab, select the Directory where your makefile is/will be. Ensure you tick the Pass build settings in environment checkbox.
  6. Select the project in the editor area. Set the correct PATH to your external libraries by going to the Build Settings > All tab, clicking + > Add User-Defined Setting, then in the first column add PATH, and in the second column add your desired path to your libraries (in my case ${PATH}:/opt/local/bin:/opt/local/sbin to append the path to libraries I installed with MacPorts).
  7. (I don't know whether this step is strictly necessary, but anyways) Click on the scheme menu then Edit scheme... or Product > Scheme > Edit scheme.... Then under Run > Arguments > Environment Variables add the PATH as in step 6.
  8. Click on the scheme menu and select the new target so that the tick mark is next to it (so that this is the active target??).
  9. Add the source files to the project by e.g. dragging and dropping them from Finder into the Navigation area. Ensure you tick the Add to target checkbox.
  10. Ensure that in your Makefile, your compiler has the -g flag set to enable debugging.
  11. Press Cmd+B to build.
  12. Once built, tell Xcode where the executable is by selecting Edit scheme... (as per step 7), then Run > Info > Executable > Other..., navigate to the project folder, and choose the command-line executable that was built in step 11.
  13. Press Cmd+R to run.

Maybe some steps are not needed, I'm not sure. I'd be happy to learn if there's a better way to do this.

Upvotes: 5

Related Questions