A closed-form formula is developed for the full magnetic gradient tensor of a polyhedral body with a homogeneous magnetization vector. It is based on the direct derivative technique on the closed form of the magnetic field. These analytical expressions are implemented into an easy-to-use C++ package which simultaneously calculates the magnetic potential, the magnetic field, and the full magnetic gradient tensor for magnetic targets. Modern unstructured tetrahedral grids are adopted to represent the polyhedral body so that our code can deal with arbitrarily complicated magnetic targets. A prismatic body is tested to verify the accuracies of our closed-form formula. Excellent agreements are obtained between our closed-form solutions and solutions of a prismatic magnetic body with differences up to machine precision. A pipeline model is used to demonstrate its capability to deal with complicated magnetic targets. This C++ code is freely available to the magnetic exploration community.