The contract is worth $45 million over six years, including $20 million in guaranteed money, reports NFL Network's Adam Schefter.
Smith, the No. 4 overall pick by Cincinnati in 2001, had 43.5 sacks in seven seasons with the Bengals and was one of the top pass rushers available on the free agent market this season.
"The reason I decided to cancel my other visits is meeting with the coaching staff here," Smith said. "From top to bottom it's a first-class organization and when you get in a situation like that it's a no-brainer. I think I'll be a great fit."
The Niners were tied for 21st in the NFL last season with only 31 sacks. Bryant Young, who led the team with 6.5 sacks, retired after the season leaving a big void at defensive end.
The Niners have been busy so far in the free agency season after going 5-11 last season, signing receiver Isaac Bruce and kick returner Allen Rossum.
The Associated Press contributed to this report