I'm using a set of classes with lots of *.h files. These files are located in a separate directory & I do not want to copy them to my own folder. I can add the path of the *.h file I use in the #include. However if that *.h file refers to another *.h file then I must change lots of #include.

BTW, I prefer not to use a makefile & do something only within *.c & *.h files :cheesy:

Dani AI

Generated

As hinted, the clean way is to teach the compiler where the headers live instead of editing every #include. On macOS you can either use a tiny Makefile or an IDE (Xcode) and set header search paths there. Below is a minimal Makefile you can drop in your project root; it adds an external include directory without changing your source files.

CC = cc
CPPFLAGS = -I/absolute/path/to/external/includes
CFLAGS = -Wall -g
LDFLAGS = -L/absolute/path/to/external/libs

SRCS = main.c foo.c
OBJS = $(SRCS:.c=.o)
TARGET = myprog

all: $(TARGET)

$(TARGET): $(OBJS)
    $(CC) $(LDFLAGS) -o $@ $(OBJS)

%.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

clean:
    rm -f $(TARGET) $(OBJS)

Notes and quick tips: put the real path in CPPFLAGS (preprocessor include flags); -I is what tells the compiler where to look. If make or a compiler is missing on macOS, install the Command Line Tools with xcode-select --install. You can also export CPATH=/path/to/includes in the environment as an alternative to editing the Makefile. Xcode users set Header Search Paths in Build Settings instead of a Makefile.

Troubleshooting: makefiles require actual TABs before commands (not spaces). If the compiler still cannot find a header, run the compiler with -v to see the include search list and verify the path. Avoid editing third-party headers; use include-paths, a wrapper header, or a small symlink into your project to keep vendor code untouched. This approach prevents changing dozens of #include lines while keeping builds reproducible for .

Recommended Answers

All 3 Replies

The best answer is to use a makefile/IDE. If you don't want to, yes, it is a lot of editing. That is precisely why you generally don't specify a path on the #include line and instead point the compiler at where to look.

The best answer is to use a makefile/IDE. If you don't want to, yes, it is a lot of editing. That is precisely why you generally don't specify a path on the #include line and instead point the compiler at where to look.

OK, then How can I use makefile on MAC?

Does your compiler have any documentation? If you let us know what compiler you have we can all Google for this information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.