简体   繁体   中英

Makefile - No such file or directory of file.h ( Can't find the solution)

I was trying to make a Makefile so I can develop my code a bit faster. My Makefile is the following:

IDIR =../include

CXX=g++

CXXFLAGS=-I$(IDIR)

LDIR =../lib

LIBS=-lm

_DEPS = person.h

DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

OBJ = main/main.o ../modules/person.o  

$(ODIR)/%.o: %.c $(DEPS)

    $(CXX) -c -o $@ $< $(CXXFLAGS)

run: $(OBJ)

    $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)

.PHONY: clean

clean:

    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

But the problem is that the terminal outputs the following message:

g++ -I./include   -c -o main/main.o modules/main.cpp
main/main.cpp:6:10: fatal error: person.h: No such file or directory
    6 | #include <person.h>
      |          ^~~~~~~~~~
compilation terminated.
make: *** [<builtin>: main/main.o] Error 1

My folder in which person.h is at ../include but for some reason, my compiler does not find it. I am using Linux and not Windows. If I use the command: $cpp -I../include/ -v so to see if there is a problem at finding my folder but the output says the following:

Using built-in specs.
COLLECT_GCC=cpp
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
COLLECT_GCC_OPTIONS='-E' '-I' '../include/' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/9/cc1 -E -quiet -v -I ../include/ -imultiarch x86_64-linux-gnu - -mtune=generic -march=x86-64 -fasynchronous-unwind-tables -fstack-protector-strong -Wformat Wformat-security -fstack-clash-protection -fcf-protection
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-
gnu/include"
ignoring nonexistent directory "../include/"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.

In which it says ignoring nonexistent directory "../include/" . I believe that this is the problem but I can't find the solution to my problem.

Thanks in advance.

(If I put all the path from user to include, I have the same problem)

EDIT: I made a file for my main and put my main.cpp in there and changed CXXLAGS to CXXFLAGS after seeing my mistakes in the comments and the answer.

EDIT2: For some reason, it worked when I renamed the folder that had my include files.

The problem is this:

OBJ = main.o ../modules/person.o  

$(ODIR)/%.o: %.c $(DEPS)
        $(CXX) -c -o $@ $< $(CXXLAGS)

So, this pattern rule tells make how to build a file that matches the pattern ../modules/%.o and it adds the CXXLAGS variable to the compile line.

But you have not provided a rule for building main.o , which does not match that pattern because it's not in ../modules/... . Because you didn't give your own rule, make uses its built-in rule, but that rule doesn't know about the variable you created CXXLAGS so it doesn't add the -I option to the compile line.

There are at least three different ways to fix this depending on what you want to do, which you haven't specified clearly.

If you really meant to put main.o into the ../modules directory then using:

OBJ = ../modules/main.o ../modules/person.o  

will make it work.

If you really want main.o to appear in the same directory you need to define a rule to create it there. You can use make's built-in rule to do that for you, but to do that you need to use make's build-in variables to control it: you are using CXXLAGS but the standard built-in variable for C++ compilation is CXXFLAGS . If you change all your uses if CXXLAGS to CXXFLAGS , then it will work.

Or you can create your own pattern rule (or explicit rule) to build main.o without using make's built-in rule.

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