At least with the compilers of which I'm aware, no, they don't generate code to automatically detect and take advantage of the available instruction set automatically (at least from completely portable code--see below for more).
Rather than two complete builds of the application, it often makes sense to move the numerical, CPU-intensive parts of the application into a DLL, and only choose those at run-time. This lets the UI and things like that (that don't really benefit from special instructions) live in a common area, and only a few specific pieces you care about get switched out at run-time.
Since you tagged C++, I'll mention one possible way of managing this: define an abstract base class that defines the interface to your numerical routines. Then define a derived class for each instruction set you care about.
Then at run-time, you have a pointer to the base. You initialization code checks the available CPU, and initializes that pointer to point to an instance of the correct type of derived object. From that point onward, you just interact with the routines via that pointer.
To avoid that adding excessive overhead, you generally want to define your functions in that class (or those classes, as the case may be) to each do quite a bit of work, to amortize the added cost of of a virtual function call over a larger amount of work.
You don't normally have to get involved with things like directly calling LoadLibrary
though. You can use the /Delayload
linker flag to tell it to only load the DLLs when you actually call a function in the DLL. Then only the DLL corresponding to the derived class(es) you instantiate will be loaded.
Another obvious possibility would be to use a library/compiler extension like Cilk Plus to manage most of the vectorizing for you. With this, you can (for example) write things like:
a[:] = b[:] + c[:];
...to get something roughly equivalent to:
for (int i=0; i<size; i++)
a[i] = b[i] + c[i];
The compiler links to the Cilk Plus library, which handles checking what instruction sets the CPU supports, and uses them as appropriate.