devxlogo

Using the C Pre-Processor

If you are porting code from C to Java it is often useful to be able to use the C pre-proccessor (cpp or cc -E). This tip shows you how to create a makefile that will allow you to run cpp on your java code. First you need to do: mkdir cpp, before you start. Assume you want to compile the classes file1.java and file2.java.

 JCC=/usr/local/java/bin/javacJFLAGS=-gCPP=gcc -EDEST=/tmp/.SUFFIXES: .class .javaTARGETS= file1.class file2.class.java.class: /bin/rm -f dummy.c ln -s $< dummy.c $(CPP) dummy.c | grep -v '^#' > cpp/$< $(JCC) $(JFLAGS) cpp/$< cp cpp/*.class /home/cameron/xg/x/classes/ cp cpp/*.class ./all: $(TARGETS)clean: /bin/rm -f *.class 

The other version allows you to just run cpp on selected files. The files that have the extension .javacpp will have the C Pre-processor run on them.

 JCC=/usr/local/java/bin/javacJFLAGS=CPP=gcc -E.SUFFIXES : .class .java .javacpp.javacpp.java: cp $< $*.c $(CPP) $*.c | grep -v '^#' > $*.java chmod -w $*.java .java.class : $(JCC) $(JAVAFLAGS) $< all: Counter.class Connect.class mv *class $(DEST)Counter.class: Counter.javaCounter.java: Counter.javacpp

Note that you need to add the dependences, otherwise when you change .javacpp, the file is still just built from the .java file.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Seasoned Architects Evaluate New Tech

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.