Using J2ObjC with Make
Stay organized with collections
Save and categorize content based on your preferences.
Here's a simple Makefile that demonstrates building with j2objc and j2objcc:
.SUFFIXES: .java .m
BUILD_DIR = build
SOURCE_DIR = src
# Change to where distribution was unzipped.
J2OBJC_DISTRIBUTION = /your/distribution/dir
J2OBJC = $(J2OBJC_DISTRIBUTION)/j2objc
J2OBJCC = $(J2OBJC_DISTRIBUTION)/j2objcc
OBJECTS = \
$(BUILD_DIR)/foo/MainClass.o \
$(BUILD_DIR)/foo/bar/Support.o \
$(BUILD_DIR)/foo/bar/Utils.o
OBJC_SOURCES = $(OBJECTS:.o=.m)
RESULT = mainclass
default: translate $(OBJECTS)
$(J2OBJCC) -o $(RESULT) $(OBJECTS)
translate: $(BUILD_DIR) $(OBJC_SOURCES)
clean:
@rm -rf $(RESULT) $(BUILD_DIR)
$(BUILD_DIR)/%.m $(BUILD_DIR)/%.h: $(SOURCE_DIR)/%.java
$(J2OBJC) -sourcepath $(SOURCE_DIR) -d $(BUILD_DIR) $?
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.m
$(J2OBJCC) -I$(BUILD_DIR) -c $? -o $@
$(BUILD_DIR):
@mkdir $(BUILD_DIR)
This works, but translation is slow because each source file is separately translated.
Like javac, it's faster to translate all related files together, since parsing the
source only needs to be done once. The following changes improves the build time significantly:
JAVA_SOURCES = $(TMPDIR).sources.list
translate: pre_translate $(OBJC_SOURCES)
@if [ `cat $(JAVA_SOURCES) | wc -l` -ge 1 ] ; then \
$(J2OBJC) -sourcepath $(SOURCE_DIR) -d $(BUILD_DIR) \
`cat $(JAVA_SOURCES)` ; \
fi
pre_translate: $(BUILD_DIR)
@rm -f $(JAVA_SOURCES)
@touch $(JAVA_SOURCES)
$(BUILD_DIR)/%.m $(BUILD_DIR)/%.h: $(SOURCE_DIR)/%.java
@echo $? >> $(JAVA_SOURCES)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThis Makefile utilizes j2objc and j2objcc to translate Java source code into Objective-C for building iOS applications.\u003c/p\u003e\n"],["\u003cp\u003eInitially, translation is slow due to individual file processing, leading to redundant parsing.\u003c/p\u003e\n"],["\u003cp\u003eOptimization is achieved by translating all related files together, significantly improving build time.\u003c/p\u003e\n"],["\u003cp\u003eThe Makefile includes rules for generating object files, linking them, and cleaning the build directory.\u003c/p\u003e\n"],["\u003cp\u003eThe second Makefile example uses a temporary file to collect all java sources to be compiled together by j2objc to speed up the process.\u003c/p\u003e\n"]]],[],null,["# Using J2ObjC with Make\n\nHere's a simple Makefile that demonstrates building with j2objc and j2objcc: \n\n .SUFFIXES: .java .m\n\n BUILD_DIR = build\n SOURCE_DIR = src\n\n # Change to where distribution was unzipped.\n J2OBJC_DISTRIBUTION = /your/distribution/dir\n J2OBJC = $(J2OBJC_DISTRIBUTION)/j2objc\n J2OBJCC = $(J2OBJC_DISTRIBUTION)/j2objcc\n\n OBJECTS = \\\n $(BUILD_DIR)/foo/MainClass.o \\\n $(BUILD_DIR)/foo/bar/Support.o \\\n $(BUILD_DIR)/foo/bar/Utils.o\n OBJC_SOURCES = $(OBJECTS:.o=.m)\n RESULT = mainclass\n\n default: translate $(OBJECTS)\n $(J2OBJCC) -o $(RESULT) $(OBJECTS)\n\n translate: $(BUILD_DIR) $(OBJC_SOURCES)\n\n clean:\n @rm -rf $(RESULT) $(BUILD_DIR)\n\n $(BUILD_DIR)/%.m $(BUILD_DIR)/%.h: $(SOURCE_DIR)/%.java\n $(J2OBJC) -sourcepath $(SOURCE_DIR) -d $(BUILD_DIR) $?\n\n $(BUILD_DIR)/%.o: $(BUILD_DIR)/%.m\n $(J2OBJCC) -I$(BUILD_DIR) -c $? -o $@\n\n $(BUILD_DIR):\n @mkdir $(BUILD_DIR)\n\nThis works, but translation is slow because each source file is separately translated.\nLike javac, it's faster to translate all related files together, since parsing the\nsource only needs to be done once. The following changes improves the build time significantly: \n\n JAVA_SOURCES = $(TMPDIR).sources.list\n\n translate: pre_translate $(OBJC_SOURCES)\n @if [ `cat $(JAVA_SOURCES) | wc -l` -ge 1 ] ; then \\\n $(J2OBJC) -sourcepath $(SOURCE_DIR) -d $(BUILD_DIR) \\\n `cat $(JAVA_SOURCES)` ; \\\n fi\n\n pre_translate: $(BUILD_DIR)\n @rm -f $(JAVA_SOURCES)\n @touch $(JAVA_SOURCES)\n\n $(BUILD_DIR)/%.m $(BUILD_DIR)/%.h: $(SOURCE_DIR)/%.java\n @echo $? \u003e\u003e $(JAVA_SOURCES)"]]