First, magma should go through an event called "crystallization". This will make sure that magma hardens up a little bit.
After this, exposure and weathering will do a thing. They will finish the process.
Carbon has the ability to form multiple bonds because it has four valence electrons. Having four valence electrons means that carbon has a lot of space to form bonds with other atoms, or multiple bonds, in order to reach the full octet.
Hope this helps!! :)