Add a field to a global set is there a way to add a field to a global set by code ? I figured out how to create a global:

$global = new GlobalSetModel();
$global-&gt;name = 'GlobalName';
$global-&gt;handle = 'globalHandle';
craft()-&gt;globals-&gt;saveSet($global)


But I can't figure out how to add a field to this newly create global.
See here for how to add a field to a user's field layout.

Users, Globals, Tags, Categories, etc. are all elements in Craft so they behave very similarly.

You can also check out Craft's own InstallService's private _createDefaultContent() method for examples on how Craft creates the default content on a fresh install of Craft.