The tiny British overseas territory nestled on the southern tip of Spain has long been a source of friction between London and Madrid, which wants it to come back under its control centuries after it was ceded to Britain in 1713.
Cameron's visit to Gibraltar later today will be the first by a British prime minister since 1968 and comes at a time of deep concern on the Rock over next week's referendum on whether to stay in or leave the European Union.
"What is being debated is whether the United Kingdom stays in the European Union -- as I hope it will -- or leaves the European Union.
"The campaign for this should be done in the United Kingdom and not in Gibraltar."
He added that whatever happened in the June 23 referendum, "for Spain, Gibraltar remains Spanish."
The British territory is eyeing the vote with increasing alarm, particularly as recent opinion polls have shown the camp in favour of leaving the EU gaining ground.
Spanish Foreign Minister Jose Manuel Garcia-Margallo, who is outspoken about his desire to see Gibraltar returning to the Spanish fold, last week revived the idea of sharing sovereignty over the Rock with Britain in the event of a Brexit.
Such a proposal had been sketched out between the two countries in 2001 and 2002, but rejected after Gibraltarians voted against it in a referendum.
Gibraltar's leader Fabian Picardo has said he is worried Spain may seize the opportunity of Brexit to threaten the land border between the two -- a long-time flashpoint in the sovereignty row.
Relations have ebbed and flowed since, but the past four years have seen tensions resurface under Spain's conservative government.
In one particularly belligerent row over disputed waters, Spanish authorities upped border checks in 2013, creating hours-long logjams and forcing the European Commission to wade in and ease the crisis.
