Using the C Pre-Processor

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 and

 JCC=/usr/local/java/bin/javacJFLAGS=-gCPP=gcc -EDEST=/tmp/.SUFFIXES: .class .javaTARGETS= file1.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 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.javacpp

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

See also  5 Strategies for Supercharging Your CRM

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.

About Our Journalist