Barcelona, Spain (CNN) -- Pope Benedict XVI defended religion from critics Sunday as he dedicated the Sagrada Familia church, a still-unfinished emblem of the Spanish city of Barcelona.
"This is the great task before us: to show everyone that God is a God of peace not of violence, of freedom not of coercion, of harmony not of discord," he said.
And he pushed back against what he sees as increasing secularism in the world, saying, "I consider that the dedication of this church of the Sagrada Familia is an event of great importance, at a time in which man claims to be able to build his life without God, as if God had nothing to say to him."
He also defended the traditional family, after Spain's Socialist government legalized same-sex marriage.
"The generous and indissoluble love of a man and a woman is the effective context and foundation of human life in its gestation, birth, growth and natural end," he said.
Spain's King Juan Carlos and Queen Sofia were in the full-to-overflowing church as the pope poured oil on the altar and rubbed it in with his hand, then swung incense over it.
He was surrounded by four rows of priests in white robes and pointed white hats, their mitres the same shape as the pope's own headdress, as he honored the architect of the church.
He praised Antoni Gaudi for "overcoming the division between human consciousness and Christian consciousness, between living in this temporal world and being open to eternal life" in designing the church.
"Gaudi did this not with words but with stones, lines, planes and points. Indeed, beauty is one of mankind's greatest needs," Benedict said, speaking in Spanish.
"Beauty also reveals God because, like him, a work of beauty is pure gratuity; it calls us to freedom and draws us away from selfishness," he said.
The pope is on his second visit to Spain, a historically Catholic country where he considers the traditional religion to be under threat.
A recent government survey showed just 74 percent of Spaniards still consider themselves Catholics, and only two in 10 regularly attend mass. The Vatican officially considers Spain to be more than 92 percent Catholic.
The Sagrada Familia itself was full for the Mass led by the pope, but the overflow area was not.
The country has largely been spared the sex abuse scandals that have rocked the church in much of the rest of western Europe.
Prime Minister Jose Luis Rodriguez Zapatero, a Socialist, is out of the country on an unannounced visit to Afghanistan. He is due to meet the pope briefly at Barcelona's airport at the end of Benedict's visit. Deputy Prime Minister Alfredo Perez Rubalcaba greeted the pope Saturday in Santiago de Compostela, the first stop of his two-day visit.
Santiago de Compostela, in Spain's northwestern tip, has been an important pilgrimage site for centuries.
The cathedral there was built 900 years ago atop what is said to be the tomb of St. James, an apostle of Jesus. After praying there, the pope presided over Eucharist in the square outside, to celebrate the city's jubilee year.
"I come as a pilgrim on this Compostelean Saint Year ... I want to join to that long line of men and women that all over the centuries had come to [Santiago de] Compostela from different places on the peninsula and Europe," the pontiff said at the airport earlier in the day.
Saturday night, the pope headed all the way across Spain to Barcelona, the Catalan city on the Mediterranean.
The Sagrada Familia, or "holy family," church, is still being built after more than 100 years.
Gaudi, a Catalan architect, only lived to see one tower and most of one facade finished by the time he died in 1926.
"The interior space of the church, the sacred space of the church, is finished, and for that, the pope comes here to consecrate the church," said Jordi Fauli, the deputy architect.
Gaudi planned the church to have 18 towers -- 12 for the apostles, four for the evangelists, one for the Virgin Mary and the tallest for Jesus.
Only eight are finished.
Fauli said the privately-financed work may be done by 2026, on the 100th anniversary of Gaudi's death.
Asked once why it was taking so long to finish the Sagrada Familia, Gaudi replied, according to his assistants, "My client -- meaning God -- is not in a hurry."
CNN's Al Goodman contributed to this report.