简体   繁体   中英

GNU make issue: “No rule to make target `pjsip.h', needed by `main.o'”

I have been working to write my first makefile and after following some tutorials and reading some links on stackoverflow I managed to write the file, what I am doing is

  • using .a files from the pjsip api, notice the variables PJSIP, PJLIB_UTIL and PJLIB
  • define the path to the .h file using the -I option, see the variables PJSIP_HDR, PJLIB_HDR and PJLIB_U_HDR

I get the following error:

 make: ***No rule to make target `pjsip.h', needed by `main.o'.  Stop.

It believe that it is usefull to mention that I have only 4 #includes in my .cpp file

#include <iostream>
#include <pjsip.h>
#include <pjlib-util.h>
#include <pjlib.h>

The goal is to have a file called main which I can excute, I am using Ubuntu 14.04 . Here is my makefile

CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
PJSIP = -L/home/ubuntu/pjsip/trunk/pjsip/lib -llibpjsip-simple-x86_64-unknown-linux-gnu.a -llibpjsip-ua-x86_64-unknown-linux-gnu.a -llibpjsip-x86_64-unknown-linux-gnu.a -llibpjsua2-x86_64-unknown-linux-gnu.a -llibpjsua-x86_64-unknown-linux-gnu.a
PJLIB = -L/home/ubuntu/pjsip/trunk/pjlib/lib -llibpj-x86_64-unknown-linux-gnu.a
PJLIB_UTIL = -L/home/ubuntu/pjsip/trunk/pjlib-utils/lib -llibpjlib-util-x86_64-unknown-linux-gnu.a
PJSIP_HDR = -I/home/ubuntu/pjsip/trunk/pjsip/include/
PJLIB_HDR = -I/home/ubuntu/pjsip/trunk/pjlib/include/
PJLIB_U_HDR = -I/home/ubuntu/pjsip/trunk/pjlib-utils/include

main: main.o
        $(CC) $(LFLAGS) main.o -o main

main.o: main.cpp pjsip.h pjlib.h pjlib-utils.h
        $(CC) $(CFLAGS) $(PJSIP_HDR) $(PJLIB_HDR) $(PJLIB_U_HDR) main.cpp $(PJSIP) $(PJLIB) $(PJLIB_UTIL)

clean:
         \rm -f *.o

Any extra comments that might help me improve my makefile are welcomed

Thanks in advance

The following rule:

main.o: main.cpp pjsip.h pjlib.h pjlib-utils.h

specifies that main.o requires all the listed files in order to be built.

You do not have any file named pjsip.h in the current directory. This is what make is telling you: this file does not exist, and there is no explicit rule to create this file.

The error message you showed is from make . make doesn't look at, and doesn't care that you pass some -I flag to some strangely-named command called g++ that gets executed to build main.o . make knows nothing about g++ , nor about any flags passed to it. All that makes knows, or cares about, are the explicitly listed targets and rules.

The problem is that you specify the pjsjp.h as dependency (ie, after the ":" in the line for main.o target); but make doesn't know how to find the include file pjsip.h - it doesn't interpret the -I ... options, these are only for the g++ command.

You could try to specify an absolute path, something like this:

PJLIB_HEADER_DIR=/home/ubuntu/pjsip/trunk/pjlib/include
main.o: main.cpp $(HEADER_DIR)/pjlib.h ...

(...and similar for other headers) By the way, the "-I" option only accepts directories (see the g++ documentation on directory search ), thus the definition of PJSIP_HDR with the header file name in it makes no sense, it should just be:

PJSIP_HDR = -I/home/ubuntu/pjsip/trunk/pjsip/include/

You probably added the header name because of the error?

The second, easier option is to simply remove those external dependencies. Just write

main.o: main.cpp

that is, leave out all the library header files. Dependecies are there to tell make to rebuild stuff if a dependency has changed. I assume those pjsip.h, pjlib.h, ... come from an external library which you are not changing often? Then making it a dependency in make isn't really necessary.

By the way, these days make files are hardly written by hand; they are typically generated by tools such as cmake .

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