Last active
May 23, 2025 16:05
-
-
Save Guiorgy/d5b02644873352c7aa9cd90545c2c31a to your computer and use it in GitHub Desktop.
Cross-compile a C/C++ project for Linux on a Windows host using Docker
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
<# | |
Copyright © 2025 Guiorgy | |
This program is free software: you can redistribute it and/or modify it under | |
the terms of the GNU General Public License as published by the Free Software | |
Foundation, either version 3 of the License, or (at your option) any later | |
version. | |
This program is distributed in the hope that it will be useful, but WITHOUT | |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
FOR A PARTICULAR PURPOSE. | |
You can see the full GNU General Public License at | |
<https://www.gnu.org/licenses/> for more details. | |
#> | |
# Check whether Docker is running | |
docker version *>$null | |
if (-not $?) { | |
Write-Error "Docker is not running" | |
exit 1 | |
} | |
# Define the project parameters | |
$project = Split-Path -Path $PWD -Leaf | |
$gcc_version = 'latest' | |
$container = "$project-gcc-dev-$gcc_version" | |
# Define the volume mapping | |
$host_dir = $PWD.Path | |
$container_dir = "/usr/src/${project}" | |
# Ensure that the container is available and configured | |
docker container inspect "$container" *>$null | |
if (-not $?) { | |
# Ensure the gcc image is available | |
$gcc = "gcc:$gcc_version" | |
docker image inspect $gcc *>$null | |
if (-not $?) { | |
Write-Output "Pulling gcc" | |
docker pull $gcc | |
} | |
# Create the container with correct mapping | |
Write-Output "Creating container `"$container`"" | |
docker run -d --name "$container" -v "${host_dir}:${container_dir}" $gcc sh -c 'trap "exit 0" TERM; tail -f /dev/null & wait' | |
# Install dependencies | |
# Uncomment the two lines below and fill them with the necessary dependencies (OpenSSL is used as an example) | |
#Write-Output "Installing dependencies: openssl" | |
#docker exec -it "$container" bash -c 'apt-get -qq update && apt-get -qq install libssl-dev' | |
# Stop container | |
docker stop "$container" *>$null | |
} | |
try { | |
# Start container | |
docker start $container *>$null | |
# Run build.sh inside the container | |
Write-Output "Executing build script" | |
# build.sh is assumed to be the script that builds the project. Replace it with the necessary command(s) | |
docker exec -w "$container_dir" "$container" ./build.sh $args | |
} finally { | |
# Stop container | |
docker stop "$container" *>$null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment