U.S. President Barack Obama is passing a diplomatic torch of sorts to Justin Trudeau — and he has a pointed warning for Canadians about the perils of injustice and inequality.

In what’s likely Obama’s last visit to Canada, the U.S. president addressed a rapt joint session of the House of Commons and the Senate, basking in a cascade of applause and heaping praise on the country’s young new prime minister.

Trudeau, said Obama, has brought new energy and leadership to Canada and to the everlasting alliance between the two countries. And he said the world will continue to benefit from Trudeau’s time in office, even as his own era comes to an end.

And just as he did during a news conference earlier in the day, Obama warned against the perils of protectionism and xenophobia, which he hinted are closer than some might think.

"If our democracies seem incapable of assuring broad−based growth, people will push back out of anger or out of fear," Obama said.

"It seems as if we draw a line around our borders, it will give us more control.... There’s just one problem — restricting trade or giving in to protectionism in this 21st−century economy will not work. It will not work."

Obama was greeted and interrupted frequently by sustained standing ovations and cheers as he entered the House of Commons, shaking hands with cabinet ministers as he took his seat, and throughout his speech.

He was introduced by Trudeau, who paid tribute to Obama’s time in office.

"History books will record signature policies, (but)what I will remember — what I hope we all will remember — are the lessons you taught us not by executive order, but by example."

Obama’s speech marked the first Commons address by a U.S. president since Bill Clinton spoke on Parliament Hill in 1995.