We present in detail the formulation of the ab initio theory we have developed for the calculation of the macroscopic second-order susceptibility Ï (2) . We find a general expression for Ï (2) valid for any fields, containing the ab initio relation between the microscopic and macroscopic formulation of the second-order responses. We consider the long wavelength limit and we develop our theory in the Time-Dependent Density-Functional Theory framework. This allows us to include straightforwardly many-body effects such as crystal local-field and excitonic effects. We compute the Second-Harmonic Generation spectra for the cubic semiconductors SiC, AlAs and GaAs and starting from the Independent-Particle Approximation for Ï (2) , we include quasiparticle effects via the scissors operator, crystal local-field and excitonic effects. In particular, we consider two different types of kernels: the ALDA and the "long-range" kernel. We find good agreement with other theoretical calculations and experiments presented in literature, showing the importance of very accurate description of the many-body interactions.