Reputation: 283
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
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).
File > New > New Project
. macOS > Command Line Tool
, Product Name etc. and language. Enter your location for the project. 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
). 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. 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). Edit scheme...
or Product > Scheme > Edit scheme...
. Then under Run > Arguments > Environment Variables
add the PATH
as in step 6. Add to target
checkbox. -g
flag set to enable debugging. Cmd+B
to build.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. 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