C/C++ Valgrind package solution for MacOS with Docker on both Apple Silicon ARM and Intel X86 architecture.
Refer: https://medium.com/@massey0ross/valgrind-in-macos-with-docker-3b0e4bbdece1
Intel X86 CPU:
Valgrind Macos Project on Github:
https://github.com/LouisBrunner/valgrind-macos/
Apple’s Silicon ARM Architecture CPU (M1, M2, M3):
Requirement:
Docker Desktop for Mac:
Docker Doc: https://docs.docker.com/desktop/install/mac-install/
Docker Desktop Installer Direct Link: Docker Desktop for Mac with Apple silicon
Introduction:
The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.1
Start Here (installed Docker Desktop)
1. Write a Dockerfile
Create a new folder / directory (mkdir) somewhere you like on you Mac and a file (touch) with name “Dockerfile” (no any file suffix).
Copy following code:
# Start image with a node base image of Ubuntu latest verison
FROM ubuntu:latest
# Install necessary package with ubuntu package manager, apt
# Caution: apt install -y parameter is necessary !!!
# To give apt install permission or docker build will fail
RUN apt update && apt install -y make gcc g++ valgrind
- make – package for Makefile
- gcc – C/C++ compiler
- valgrind – C/C++ debugg and test tool
2. Build a docker image with docker build command
Open terminal and navigate to the directory where your Dockerfile locate and run following docker build command to build a docker image
docker build -t "valgrind:1.0" .
Docker build parameter meaning:
-t "image name"
give the image a tag (or can be considered as name) to identify
.
ending with dot is mandatory for specifying working directory
The single dot (
Docker Docs: https://docs.docker.com/build/building/packaging/.
) at the end of the command sets the build context to the current directory. This means that the build expects to find the Dockerfile and the file in the directory where the command is invoked.
This will create an image based on Ubuntu Linux, and install make gcc g++ valgrind and all other packages you specified on Dockerfile onto it
3. Docker Run to docker container
Open terminal and navigate to the directory where your C/C++ source files locate and run docker run command to start a container
docker run -it --name valgrind -v $PWD:/tmp -w /tmp valgrind:1.0
Docker run parameter meaning:
-i
or --interactive
: allows you to interact with the running container through the terminal
-t
or --tty
: provides the terminal or command line interface you can interact with
--name
: give a name to container to identify
-v /local/host/directory:/container/directory
: mount a directory inside container as volume
$PWD
: text alias to current working directory
-w
: specify /bin/bash working directory (/tmp where the directory we mounted)
valgrind:1.0
: image tag name
Done! Congrats! Enjoy valgrind on your Mac now!
root@containerID:/tmp#
Reminder:
Built C/C++ file may not run in Ubuntu based container2. You need to rebuild it with make again ignoring time stamp.
make -B
Reference:
- Valgrind Project, https://valgrind.org/ ↩︎
- Makefile force rebuild, https://stackoverflow.com/questions/816370/how-do-you-force-a-makefile-to-rebuild-a-target ↩︎
2 responses to “C/C++ Valgrind MacOS Solution -Docker”
Can you write more about it? Your articles are always helpful to me. Thank you!
I do agree with all the ideas you have presented in your post. They’re very convincing and will certainly work. Still, the posts are very short for beginners. Could you please extend them a little from next time? Thanks for the post.