Barack Obama wrapped up a lightning visit to Spain on Sunday, an abbreviated first presidential trip to the country overshadowed by the fatal shooting of five police officers in Dallas.
Obama squeezed in the trip after a key NATO summit, before dashing home to deal with the aftermath of the wrenching attack.
Greeting Spain King Felipe at the Royal Palace in Madrid, Obama acknowledged that "we`ve had a difficult week in the United States."
The US president cut a two-day visit to Spain down to one and cancelled a trip to Seville with the king altogether, after a black army veteran killed five white police officers.
Obama, who prides himself on his measured approach, is usually loath to alter travel plans.
But the shooting -- just blocks from the site where John F. Kennedy was assassinated -- has pushed a country crackling with political tension to the edge.
Obama, speaking in Madrid, renewed his appeal for calm and cautioned those protesting police killings of black Americans against blanket criticism, saying the majority of officers do a good job.
"If the rhetoric does not recognize that, then we`re going to lose allies in the reform cause," he said.
On Saturday, speaking in Warsaw just before Air Force One departed for Spain, Obama insisted the country`s divisions were overplayed.
This was not, he insisted, the crisis-ridden era of the 1960s, when US cities burned, the Vietnam War raged and the Kennedy brothers and Martin Luther King were slain.
But his decision to shorten the trip is a tacit acknowledgement that the United States faces a combustible mix of deteriorating race relations, hyper-charged election politics and seemingly never-ending gun violence.
In recent months, videos of deadly mass shootings, or of white police officers killing black civilians, have become routine.
"Whenever those of us who are concerned about fairness in the criminal justice system attack police officers, you are doing a disservice to the cause," Obama said in Madrid, as protests against police brutality continued around the country.Obama will attend an interfaith service in honor of the five slain officers on Tuesday.
Before leaving Spain, he visited a US and Spanish Naval Station at Rota, a base at the mouth of the Mediterranean, and addressed US personnel stationed there.
Since the time of the Greeks, Romans, Moors, Phoenicians and Visigoths, the Bay of Cadiz has been seen as a prime piece of geopolitical real estate.
During the Cold War nuclear weapons were positioned there. Today, Rota has taken on renewed importance with the deployment of four US Aegis destroyers.
They form an integral part of a guided European missile defense system much despised by Russia.
The transfer of the system to NATO control was a key part of an alliance summit in Warsaw which stressed deterring Moscow from destabilizing eastern Europe.
Obama thanked the US personnel for their service and listed all the challenges NATO allies faced, from terrorist attacks to "Russia`s aggression against Ukraine" which he said threatens peace in Europe.
He also boarded the US Ross, one of four destroyers temporarily stationed in Rota that launch guided missiles, to greet uniformed soldiers waiting at a red carpet flanked by decorative wooden missiles.Obama`s visit comes as Spain remains mired in a months-old political crisis, with two general elections resulting in no clear victor.
The two centre-left and centre-right parties that have dominated Spanish politics since the return of democracy in the late 1970s have, in the face of insurgent leftists, so far been unable to form a government.
Obama met acting premier Mariano Rajoy as well as opposition leaders.
Rajoy`s conservative Popular Party (PP) won a repeat general election on June 26 but fell short of an absolute majority and needs to support of other parties to form a government.
"I will make all necessary efforts to quickly form a government," Rajoy said to reporters after meeting Obama.
Meanwhile, dozens of people braved the heat Sunday at noon to protest outside the US embassy, some carrying signs against the TTIP free trade deal being negotiated between the US and EU, and banners that read "Obama go home" or "War".