There is no one duplicate of your question, though most parts of the answer can be found in other questions, such as
When you do Insert --> Program Listing, or Insert --> File --> Child document --> Program listing, LyX uses the features of the listings package to typeset the source code. The listings output can be customized in many different ways, which are described in the manual.
You can add customizations for each listing, or do it globally. To do it for a single listing, right click it and choose Settings. If it is a child document, add options to the More parameters box. If it is plain listing, you'll find this same box under the Advanced tab in the settings. To do it globally, which is likely what you want, go to Document --> Settings --> Listings.
Parameters are entered as key=value pairs, just as you see in the questions linked to above, but in the LyX settings you add one parameter per line, commas are not needed. Syntax highlighting is covered by the first of the questions above. To add line numbering, add numbers=left, which adds the numbers to the left of the line, and you may want something like numberstyle=\tiny which sets the numbers in a small size.
A complete set of parameters, that you can copy-paste to Document --> Settings --> Listings, is
basicstyle={\ttfamily}
commentstyle={\color{green}\ttfamily}
keywordstyle={\color{blue}\ttfamily}
language={C++}
morecomment={[l][\color{magenta}]{\#}}
numbers=left
numberstyle={\tiny}
stringstyle={\color{red}\ttfamily}
You may also be interested in columns=fullflexible and breaklines=true.
listingspackage. – Torbjørn T. May 30 '14 at 18:57listingsalso has the ability to display line numbers, add the optionnumbers=left. (Meant to add this sooner, but forgot, sorry about that.) – Torbjørn T. Jun 03 '14 at 10:57