Closing a historic visit to Africa, President Obama on Tuesday urged the continent's leaders to prioritize creating jobs and opportunity for the next generation of young people or risk sacrificing future economic potential to further instability and disorder. 

He said the "urgent task" of generating jobs for a population that is expected to double to around 2 billion people in the coming decades will be "an enormous undertaking." But he said it can be achieved with U.S. help. 

"Africa will need to generate millions more jobs than it is doing now," Obama said in a speech to the entire continent delivered from the headquarters of the African Union, a member organization of African nations. "And time is of the essence." 

"The choices made today will shape the trajectory of Africa -- and therefore the world -- for decades to come," said Obama, who is seen by the people of Africa as one of their own. It was the first speech to the AU by a sitting American president. 

The speech marked the end of Obama's five-day visit to Africa that included an earlier stop in Kenya, homeland of his late father. 

Obama also called on Africa's leaders to make their countries more attractive to foreign investment by cleaning up corruption, upholding democratic freedoms, supporting human rights, and willingly and peacefully leaving office when their terms expire. 

Obama, who is more than halfway through his second and final term, said "I just don't understand" the phenomenon of leaders who refuse to step aside when their terms end. He referred to Burundi's leader, who was just elected to a controversial third term although he is constitutionally limited to two. The announcement that President Pierre Nkurunziza was seeking a third term sparked days of unrest across the country. 

"There's still so much I want to get done to keep America moving forward. But the law is the law and no one is above it, not even presidents," Obama said. "And, frankly, I'm looking forward to life after being president. It will mean more time with my family, new ways to serve, and more visits to Africa." 

He called on the AU to use its authority to help make sure African leaders stick to their term limits and follow their constitutions. "No one should be president for life," said Obama, who leaves office in January 2017. 

Africa's progress will also depend on security and peace, since businesses and wealthy people won't want to invest in unsafe places, the president said. 

He pledged continued U.S. training assistance and other support in the fight against terrorism carried out across the continent by groups like al-Qaida, the Islamic State, al-Shabab and Boko Haram. He said the world must do more to help, too, and announced that he will host a summit at the United Nations in September to secure additional support for international peacekeeping, including in Africa. 

Obama said Africa's impending population boom could bring tremendous opportunities for the continent on the one hand. 

"On the other hand," he said, "we need only look to the Middle East and North Africa to see that large numbers of young people with no jobs and stifled voices can fuel instability and disorder." 

Before addressing the AU, Obama highlighted his administration's efforts to combat hunger by touring a Faffa Foods factory that participates in the U.S. Feed the Future program. The initiative focuses on helping smaller farmers in 19 countries, including Ethiopia and 11 other African nations, expand their businesses. 

He also met one-on-one with Nkosazana Dlamini-Zuma, chairperson of the African Union Commission, who introduced him at AU headquarters. 

"Although we welcome you as the president of the United States of America, we also claim you as our own," she said. 

Obama's speech closed a homecoming of sorts to Africa. 

The president first flew to the Kenyan capital of Nairobi to attend a U.S.-sponsored business development summit, but he also spent time reconnecting with relatives on his father's side of the family, including his sister, Auma Obama, and a grandmother. 

Kenyans had waited years to welcome him back as president and many lined the streets in Kenya, as well as in Ethiopia, hoping for a glimpse of him as his motorcade drove by. 

Obama, the first sitting U.S. president to visit both countries, arrives back in Washington early Wednesday.