Created
February 24, 2026 11:01
-
-
Save ABD-01/b5e9d1afe35250790a2655a1c4d3f428 to your computer and use it in GitHub Desktop.
Using NDK to build ARM aarch64 ELF and running on Android device via adb shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <arpa/inet.h> | |
| #include <netinet/in.h> | |
| #include <sys/socket.h> | |
| #include <unistd.h> | |
| #include <iostream> | |
| #include <cstring> | |
| int main() { | |
| const char* server_ip = "13.126.18.223"; // server IP | |
| int port = 11024; | |
| int sock = socket(AF_INET, SOCK_STREAM, 0); | |
| if (sock < 0) { | |
| perror("socket"); | |
| return 1; | |
| } | |
| sockaddr_in serv_addr{}; | |
| serv_addr.sin_family = AF_INET; | |
| serv_addr.sin_port = htons(port); | |
| inet_pton(AF_INET, server_ip, &serv_addr.sin_addr); | |
| if (connect(sock, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { | |
| perror("connect"); | |
| close(sock); | |
| return 1; | |
| } | |
| const char* msg = "Hello from Android NDK TCP client!\n"; | |
| send(sock, msg, strlen(msg), 0); | |
| char buffer[1024] = {0}; | |
| int n = recv(sock, buffer, sizeof(buffer)-1, 0); | |
| if (n > 0) { | |
| buffer[n] = '\0'; | |
| std::cout << "Server replied: " << buffer << std::endl; | |
| } | |
| close(sock); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NDK_PATH := D:/android_sdk/ndk/25.1.8937393 | |
| API_LEVEL := 21 | |
| ABI := arm64-v8a | |
| BUILD_DIR := build | |
| SRC_DIR := . | |
| CXX := $(NDK_PATH)/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe | |
| CC := $(NDK_PATH)/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe | |
| TARGET_TRIPLE := aarch64-none-linux-android21 | |
| SYSROOT := $(NDK_PATH)/toolchains/llvm/prebuilt/windows-x86_64/sysroot | |
| OPT_LEVEL := O0 | |
| DEBUG_FLAG := -g | |
| CFLAGS := --target=$(TARGET_TRIPLE) --sysroot=$(SYSROOT) -fPIE -std=gnu99 -$(OPT_LEVEL) $(DEBUG_FLAG) | |
| CXXFLAGS := --target=$(TARGET_TRIPLE) --sysroot=$(SYSROOT) -fPIE -std=c++17 -$(OPT_LEVEL) $(DEBUG_FLAG) | |
| LDFLAGS := --target=$(TARGET_TRIPLE) --sysroot=$(SYSROOT) -fPIE -pie -static-libstdc++ | |
| # SRCS_C := $(SRC_DIR)/api.c | |
| SRCS_CPP := $(SRC_DIR)/main.cpp | |
| SRCS := $(SRCS_C) $(SRCS_CPP) | |
| OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS_C)) | |
| OBJS += $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS_CPP)) | |
| TARGET := $(BUILD_DIR)/main | |
| all: $(TARGET) | |
| $(TARGET): $(OBJS) | |
| @mkdir -p $(dir $@) | |
| @echo "Linking: $@" | |
| $(CXX) $(OBJS) $(LDFLAGS) -o $@ | |
| $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | |
| @mkdir -p $(dir $@) | |
| @echo "Compiling C: $< -> $@" | |
| $(CC) $(CFLAGS) -c $< -o $@ | |
| $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp | |
| @mkdir -p $(dir $@) | |
| @echo "Compiling C++: $< -> $@" | |
| $(CXX) $(CXXFLAGS) -c $< -o $@ | |
| push: | |
| cmd //C "D:/android_sdk/platform-tools/adb push build/main /data/local/tmp" | |
| D:/android_sdk/platform-tools/adb shell "chmod +x /data/local/tmp" | |
| adb: | |
| D:/android_sdk/platform-tools/adb shell | |
| clean: | |
| @echo "Cleaning up..." | |
| rm -rf $(BUILD_DIR) | |
| .PHONY: all clean | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment