简体   繁体   中英

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 foo.o as expected.

But when I run make , 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?

Thanks

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.

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