User Tools

Site Tools


make:makefile-java-and-c

This is an old revision of the document!


Makefile to compile java and c program

Java class needs to call through jni the library compiled from the c program.
The following Makefile will compile / run Hello.java and hello.c

Makefile
# Names of the Java class and C source files
JAVA_CLASS = Hello
C_SOURCE = hello.c
 
# Name of the shared library (different between Windows and Unix)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
    LIB_NAME = libhello.so
endif
ifeq ($(UNAME_S),Darwin)
    LIB_NAME = libhello.dylib
endif
ifeq ($(UNAME_S),Windows)
    LIB_NAME = hello.dll
endif
 
# Compilers for C and Java
CC = gcc
JAVA_HOME = $(shell readlink -f /usr/bin/javac | sed "s:/bin/javac::")
JAVA_INCLUDE = -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux # Modify this depending on the OS
 
# Options for the C compiler
CFLAGS = -shared -fPIC
 
# Defining targets
.PHONY: all clean
 
all: $(JAVA_CLASS).class $(LIB_NAME)
 
# Compiling the Java class and generating the JNI header
$(JAVA_CLASS).class: $(JAVA_CLASS).java
	javac -h . $(JAVA_CLASS).java
 
# Compiling the C code into a shared library
$(LIB_NAME): $(C_SOURCE) $(JAVA_CLASS).class
	$(CC) $(CFLAGS) $(JAVA_INCLUDE) -o $(LIB_NAME) $(C_SOURCE)
 
# Running the Java program
run: all
	java -Djava.library.path=. $(JAVA_CLASS)
 
# Cleaning up generated files
clean:
	rm -f $(JAVA_CLASS).class $(JAVA_CLASS_NAME)_*.h $(LIB_NAME)
make/makefile-java-and-c.1705346366.txt.gz · Last modified: 2024/01/15 21:19 by odefta