One Two Three One Two Three - 7 days ago 5x
C++ Question

Why doesn't my makefile with include header work?

I have the following makefile (for c++)

LDLIBS=$(shell root-config --libs)
INCLUDE= -I/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/include \
-I/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/include/darwin \

foo: foo.o
$(CXX) -shared -fPIC $(LDLIBS) $(INCLUDE) -o foo.o foo.cpp

foo.cpp has the following includes

#include <jvmti.h>

If I run the
"g++ -shared -fPIC -I..."
command manually, it'll produce the
as expected.

But when I run
, I'll get this error

$ make
c++ -c -o foo.o foo.cpp
lib_track_npe.cpp:1:10: fatal error: 'jvmti.h' file not found
#include <jvmti.h>
1 error generated.
make: *** [foo.o] Error 1

Could someone please tell me what I did wrong in the makefile?



The implicit make rule for building .o targets from .cpp sources does not use the INCLUDE variable. INCLUDE is not a standard variable used by default make rules. Your Makefile is dependent on the default make rules in order to build .o targets from .cpp sources.

The correct make variable for specifying preprocessor options is CPPFLAGS:

CPPFLAGS= -I/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/include            \
         -I/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/include/darwin     \

Additionally, your explicit make rule for linking foo from foo.o specifies all these -I optionals. Unfortunately, that accomplishes absolutely nothing, whatsoever. -I is used only when compiling .cpp sources. The -I option is not used at all, when linking, and is effectively ignored when linking. You should simply remove the $(INCLUDE) from your link command, without even replacing it with $(CPPFLAGS). It only causes confusion.