I have a cylinder in a 3D world. My cylinder is defined as follow: Point A and Point B, Radius R
From a given point P in space, I would like to get the closest point X on the cylinder
I have found, and implemented the algo point to line, thanks to this thread: https://diego.assencio.com/?index=ec3d5dfdfc0b6a0d147a656f0af332bd
But I don't know how to apply that to a cylinder... Thanks for help !

Sketch:
The (finite) cylinder that you're considering has three parts, the top, bottom, and the lateral side. One approach would be to find the distance to the top, bottom, and the side and take the minimum of the distances (and corresponding points).
$1$) Closest point is in the middle of the lateral side. In this case, you can find the closest point on the axis line, call this point $Q$. Then, the closest point is the intersection of the cylinder with the segment between $P$ and $Q$. (Alternatively, take the point of distance $r$ from $Q$ in the direction from $Q$ to $P$.)
$2$) Closest point is in the middle of the top (or bottom, due to symmetry). In this case, closest point is the orthogonal projection onto the plane that contains the top of the cylinder.
You know that Case $1$ is a possibility when the closest point to $P$ on the axis line is within the cylinder. You know that Case $2$ is a possibility when the orthogonal projection lies within the disk that is the top (or bottom) of the cylinder. If either of these two conditions fails, then you also need to check the place where the lateral side meets the top or bottom. In other words, you need to find the distance between $P$ and the circles at the top and bottom of the cylinder.
$3$) The closest point on the circles at the top (or the bottom, due to symmetry) of the cylinder. This point can be found by projecting $P$ onto the plane containing the circle and then taking the closest point on the circle to the projection. This only fails when $P$ lies on the axis of the cylinder, but in this case, the closest point is either a center of a disk at the end or there are infinitely many closest points on the side of the cylinder.