Skip to content

Commit ce1c704

Browse files
Update build.sh
1 parent 3cdc730 commit ce1c704

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

build.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ print_help() {
4949
echo ""
5050
echo "Options:"
5151
echo " -h, --help Show this help message and exit."
52-
echo ""
53-
echo "Examples:"
54-
echo " ./build.sh build # Build for host architecture (will prompt for target)"
55-
echo " ./build.sh build aarch64 # Build for ARM64 (will prompt for target)"
56-
echo " ./build.sh clean # Clean build files"
57-
echo " ./build.sh cross arm-linux-gnueabihf-gcc armv7l # Cross-compile for ARMv7"
5852
}
5953

6054
# --- Handle help command early ---
@@ -274,7 +268,6 @@ build() {
274268
fi
275269
fi # End of CI/interactive build type selection
276270

277-
log_info "Compiling ${SRC_FILE} for ${TARGET_ARCH} using ${COMPILER}..."
278271
log_info "Initial compiler flags: ${CFLAGS}"
279272

280273
# Special handling for different architectures or cross-compilation
@@ -402,11 +395,23 @@ build() {
402395
log_info "Final compiler flags: ${CFLAGS}"
403396
log_info "Final linker flags: ${LDFLAGS}"
404397

405-
# Compile the source file
406-
"${COMPILER}" ${CFLAGS} ${TARGET_SPEC} "${SRC_FILE}" -o "${BUILD_DIR}/${BIN_NAME}" ${LDFLAGS}
398+
# --- Two-step build process: Compile to .o, then link .o to executable ---
399+
400+
# Step 1: Compile source file to object file
401+
log_info "Compiling ${SRC_FILE} to object file: ${BUILD_DIR}/${BIN_NAME}.o"
402+
"${COMPILER}" ${CFLAGS} ${TARGET_SPEC} -c "${SRC_FILE}" -o "${BUILD_DIR}/${BIN_NAME}.o"
403+
404+
if [ $? -ne 0 ]; then
405+
log_error "Compilation to object file failed!"
406+
exit 1
407+
fi
408+
409+
# Step 2: Link object file to create the executable
410+
log_info "Linking object file ${BUILD_DIR}/${BIN_NAME}.o to create executable: ${BUILD_DIR}/${BIN_NAME}"
411+
"${COMPILER}" "${BUILD_DIR}/${BIN_NAME}.o" -o "${BUILD_DIR}/${BIN_NAME}" ${LDFLAGS}
407412

408413
if [ $? -eq 0 ]; then
409-
log_success "Compilation successful!"
414+
log_success "Compilation and Linking successful!"
410415
log_info "Executable created at: ${BUILD_DIR}/${BIN_NAME}"
411416
log_info "To run: ./${BUILD_DIR}/${BIN_NAME} <image-file>"
412417
log_info "Example: ./${BUILD_DIR}/${BIN_NAME} assets/test.jpg"

0 commit comments

Comments
 (0)