Attaching shaders to multiple programs

Can one compiled shader be attached simultanously to a number of programs? Say, something like this:

glCompileShader(ID);
glAttachShader(program1, ID);
glLinkProgram(program1);
glAttachShader(program2, ID);
glLinkProgram(program2);

And then just switching from one program to another on demand? I have a little library containing functions used in all the other shaders and having it compiled multiple times doesn’t make much sense.

Yes you can. Actually you can even delete the shader after the program get compiled and link.

There is no more good use for it after linking point

Yep, that’s what specs say, but in real life things are different…

glCompileShader does not really compile your shader. All shaders attached to a program are compiled and linked when you call glLinkProgram.

Still, I do it exactly the way you described - if some program uses shader that I allready loaded, then I’ll just attach it - no need to create new shader object.

On GeForce, you can delete shader object after you linked your program object, but on Radeon, deleting shader object will damage all program objects that use it. So, no - you can’t delete shader object after you linked your program.
This information may not be up to date - perhaps AMD has fixed this issue allready, but then again - you may run into user that hasn’t updated his drivers recently.