Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

make a .sh script to compile my C program using gcc to diffrent architectures. here;s…

  1. User

    make a .sh script to compile my C program using gcc to diffrent architectures. here;s the base command: gcc ./bot/main.c -o ./output/bot.bin

    replace the bin with the architecture and "bot" with a string you can change in the script.

  2. Assistant

    Sure! Here's an example of a script that can compile your C program using different architectures:

    #!/bin/bash
    
    # Function to compile the C program
    compile_program() {
        ARCH="$1"
        OUTPUT_DIR="./output"
        PROGRAM_NAME="bot"
        
        # Construct the output binary filename
        BINARY_FILE="${OUTPUT_DIR}/${PROGRAM_NAME}.${ARCH}"
        
        # Compile the C program with the specified architecture
        gcc -march="${ARCH}" ./bot/main.c -o "${BINARY_FILE}"
    
        # Check if compilation was successful
        if [[ $? -eq 0 ]]; then
            echo "Compilation successful! Binary file created: ${BINARY_FILE}"
        else
            echo "Compilation failed!"
        fi
    }
    
    # Example usage:
    compile_program "x86"      # Compile for x86 architecture
    compile_program "armv7l"   # Compile for ARMv7 architecture
    

    To use this script, save it as compile.sh, make it executable (chmod +x compile.sh), and then run it using ./compile.sh. The compile_program function is called twice in this example, first with the x86 architecture and then with the armv7l architecture. You can add more architecture names and customize the function as per your requirements. The compiled binary files will be saved in the output directory, with filenames including the respective architecture.