Hello im trying to make a simple makefile for c++ on MacOS
I want to compile a code that uses these libraries
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <portaudio.h>
Im trying to use this makefile but I don't know how to add the libraries.
.PHONY: all clean
CC:= clang++
CFLAGS = -std=c++11 -Wno-deprecated-enum-enum-conversion -O2
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,%.o,$(SOURCES))
DEPENDS := $(patsubst %.cpp,%.d,$(SOURCES))
all: main
clean:
$(RM) $(OBJECTS) $(DEPENDS) main
-include $(DEPENDS)
# Make object files
%.o: %.cpp Makefile
$(CC) $(WARNING) $(CFLAGS) -MMD -MP -c $< -o $@
# Linking the executable from the object files
main: $(OBJECTS)
$(CC) $^ $(LDFLAGS) -o $@
./main
This isn't necessarily about your Makefile, as you said you get a red line. That's your IDE. So what you do is going to depend upon your IDE.
Part of the problem is that depending on how you installed your compiler, stuff can land in weird places.
I bet if you look, you don't have a /usr/include directory. You can do something like this:
find / -name stdlib.h 2>/dev/null
This will give you hints where the include files are. You can then chase that upwards to see if somewhere you have a symbolic link you should use.
On my Mac, I see gcc is installed in /usr/local/celler/gcc/version/c++/version... Whew. I didn't go looking for any symlinks, but I have no /usr/include.
I also have /Library/Developer/CommandLineTools/usr/include/c++/v1...
You might fix this by manually creating a symlink from /usr/include to one of those locations. There might be a better way to do that.
Or you might want to add -I options that your IDE recognizes. You'll need those in your Makefile then.
This is one of the annoying things about programming C++ on a Mac. Stuff works, but the installations don't necessarily work straight out of the box without a few games. Or it's possible they get set up correctly, but then MacOS erases stuff if you do an OS upgrade. (I'm pretty sure that Mac was configured properly at one point, but g++ can't find include files again.).
You may also have to use clang instead of g++.