I'd also want to draw attention to the fact that civil and criminal law frequently outlaw conduct - such as suicide, or prostitution, or drug use, or abortion, when there is little chance of the law being universally enforced.The roles of the law are clearly to provide a discouragement to the behaviour plus signal the society's view of it - the only possible justification for a law against suicide!
As others have pointed out, there are bound to be breaches but the important thing is that they are regarded as punishable breaches, not matters of no importance, or, worse still, a set policy - and that genuine efforts are made to prosecute offenders.
Of course self-interest has a role in the setting of rules, but I think it is on a macro scale. It is everyone's interest that there are rules of war as opposed to open slather. Wellington's policy in the Peninsula both encouraged the civilians to support/assist him while discouraging the use of guerilla tactics against him, since there was no advantage to be gained by doing so. And there must be discipline within an army, or all you have got is what only too often appears, including in some of the more appalling conflicts of today - large numbers of armed men, trained to kill, running amok.
Cheers
C