How to build relationships with professors in such a way that results in good letters of recommendation? I know a lot of questions are asked about letters of recommendation, but I was unable to find anything on this site or the internet in general that spoke to the particular issue of how to build the sort of relationship with a professor that results in good letters of recommendation. I get that you should of course do pretty well in the class and participate and go to office hours and be generally easy to work with and stuff (and ideally do research with the professor), but apart from that, is there nothing more you can do?

I know some might be eager to answer that you shouldn't be so mercenary about it (i.e. you should just organically be interested in the material and the professor and the good letter of recommendation will follow), so allow me to provide an example where this did not work out. I got a letter of recommendation from my favorite professor in college, which for some reason I was able to see (it was about a decade ago, so I forget how; I had waived my right to see it). Even though I thought he liked me (I went to office hours multiple times; I had two classes with him, both of which I did well in; he encouraged me to go for a fellowship; he even went out of his way to see a play I was in), the letter was the most basic "this student was in my class" sort of letter you could possibly imagine. It was such an incredible slap in the face. 

Maybe he was just a jerk and that's that. Maybe I should have asked for a "strong" letter of recommendation (which I didn't know at the time was what you were supposed to ask). But regardless, given the vagaries of human relationships on top of the limitations of your own abilities, how is it even possible to get a strong letter of recommendation?
Strong recommendation letters come from professional relationships, rather than personal relationships.  Of course, the two are often related, but if a professor hasn't seen much more than getting an A in a class from a professional perspective, then they can't say that they have.  A wise and helpful faculty member will warn you of this fact in advance, but faculty are human and many may not be wise enough or  helpful enough to give an appropriate warning.

The canonical answer, of course, for getting a good recommendation to grad school is to work with a professor on an actual research project.  What you actually need, however, is for a professor to be able to speak to your potential to become a good researcher.  Even working on a research project will not necessarily demonstrate that, if your role is as a cog in somebody's machine (a friend once worked on a research project as an undergraduate where her job was literally to grind rocks into powder with a mortar and pestle: the project was awesome, the job they wanted done was not).

What does research potential really consist of?  In my experience, research potential means:


Creativity in approaching difficult or ill-defined problems
Initiative enough to accomplish hard work work independently
Skill enough to solve complex problems effectively
Intelligence and background enough to acquire new skills when needed
Social skills enough to do all of these things as part of a team (semi-optional)


There is another excellent way besides actual research projects to demonstrate all of these as an undergraduate: project-centric classes.  Many graduate or upper-level undergraduate classes have an large optional or required capstone project.  When you take on such a project, it gives you a chance to do something really cool, above and beyond the base minimum.  With a good project, you can demonstrate all of these qualities to your professor and provide both the evidence and the motivation to write a really good letter.