简体   繁体   中英

How to generate dependencies with header files from different directories using a Makefile?

I'm trying to use the ASIO library for a networking project. My project and the ASIO header files are located in different directories. Compilation of my main.cpp (which does nothing but include asio.hpp and iostream ) works just fine, but the make depend command yields an error saying that asio.hpp was not found. How might I fix this?

CC = g++
ASIO = -I /Users/user1/Desktop/SDK/asio-1.18.0/include
CFLAGS = -Wall -g -std=c++11 ${ASIO}

SRCS = main.cpp
#SRCS = ${wildcard *.cpp}
OBJS = ${SRCS:.cpp=.o}
INCLS = ${SRCS:.cpp= main.h}

a.out: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS)

$(OBJS):
    $(CC) $(CFLAGS) -c $*.cpp

depend: Makefile.dep
    $(CC) -MM $(SRCS) > Makefile.dep

Makefile.dep:
    touch Makefile.dep

clean:
    rm -f $(OBJS) a.out core a.exe

include Makefile.dep

It's not so mysterious: the option describing where to find headers is here:

ASIO = -I /Users/user1/Desktop/SDK/asio-1.18.0/include

and the rule to build dependencies is here:

depend: Makefile.dep
        $(CC) -MM $(SRCS) > Makefile.dep

Notice anything missing? The option used to find headers isn't present in that command. Change it to:

depend: Makefile.dep
        $(CC) $(ASIO) -MM $(SRCS) > Makefile.dep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM