Over the last year Gibraltar found itself bumped up the news agenda. Last July, a seemingly innocuous decision to extend an artificial reef in Gibraltarian waters caused a diplomatic rumpus. Spain slowed down customs checks to create six-hour delays at the border, threats were made to charge €50 to enter Gibraltar, Royal Navy warships set sail for the Mediterranean, while Ian Paisley Jr helpfully told the Spanish ambassador in London to "pack his sombrero, sangria and straw donkey and go". Meanwhile, Channel 5's documentary Gibraltar: Britain in the Sun received unexpectedly high ratings. As the Scottish referendum draws closer, this strange outcrop of rock that actually wants to stay British takes on an extra fascination.

But what of Gibraltar from a literary perspective? Ever since an Anglo-Dutch force captured the Rock in the war of the Spanish succession, few writers have dared to visit, and those who have don't seem to have liked what they found. Daniel Defoe created some pamphlets about it in the 1720s; mostly, though, he complained about the food: "Mutton from the Barbary Coast … poor thin stuff without any fat … The wine was thought cheap because five pence a pint, but was at the same time so miserably bad, that in England we should have thought it dear at two pence a quart ..."

Samuel Taylor Coleridge stopped off in 1804, en route for a new job with the governor of Malta. "I felt myself ill," he wrote in his Notebooks, "a sense of suffocation and convulsive snatches in my stomach and limbs … I could not eat breakfast – my tongue furry-white, my pulse quick and low and my nostrils haunted by fever smells ..."

William Thackeray's account begins promisingly: "At evening," he wrote after a visit in 1844, "the place becomes quite romantic." Then comes the killer line: "It is too dark to see the dust on the dried leaves." He goes on to issue what sounds like a warning to writers, describing "sentries marching everywhere, and (this is a caution to artists) I am told have orders to run any man through who is discovered making a sketch of the place." Even Mark Twain, that most companionable of travellers, described Gibraltar in 1869 as "suggestive of a 'gob' of mud on the end of a shingle".

Perhaps it is the unambiguous nature of the military fortress that sounded the death knell for creative thought. In 1782, Mozart was commissioned to write a symphony celebrating the siege of Gibraltar. He gave up, declaring the subject matter "too bombastic".

The Rock has fared little better in the 20th century. Arguably the greatest Gibraltar novel of all is A Vision of Battlements by Anthony Burgess. Burgess was stationed there in the second world war and his loathing for the place – "an emblem of waste and loneliness" – pours from his writing: "carious yellow stucco, wooden lattices shutting in bugs, stink, babies". The book, though, is superb – a sort of military Lucky Jim shot through with squalid Burgessian nastiness. In his autobiography, Burgess takes to task another writer who tackled Gibraltar – his idol, James Joyce. The last chapter of Ulysses is larded with Molly Bloom's sultry reminiscences of her childhood "and the jessamine and geraniums and cactuses and Gibraltar as a girl where I was a Flower of the mountain". However, as Burgess points out, Joyce should have made Molly's father a sergeant major rather than a major, andher mother Gibraltarian rather than Spanish. "What is curious," Burgess concludes with not a little glee, "is the fact that Joyce the traveller never showed any desire to visit the Rock."

So why, you might ask, would anyone choose to research a novel in Gibraltar? My Damascene moment came 10 years ago, in the unlikely setting of a law conversion course in London. In a rare break, a lecturer posed a question: "Which city has the highest proportion of lawyers per capita in the world?" A few hands went up at the front. New York? Tokyo? "Gibraltar," came the answer. Really, I thought, remembering a trip there in my youth – Rock, Union Jacks, Barbary apes. But lawyers?

A few months later, exams over, I found myself on an a flight to Gibraltar. Immediately, the reason for the legal invasion became clear. The military have all but deserted the Rock, leaving Gibraltarians in need of new employment. The local government has lowered corporation tax, and high finance has come storming in – online gaming companies, hedge funds, private banks. I sat in on a few cases at the supreme court and had some informal interviews with local law firms. Already, my mind was wandering – what would it be like to grow up here, one of 30,000 residents in an area no bigger than London's Hyde Park? With Spain threatening to swallow you up and London, threatening to give you away?

I started to explore. The Barbary apes roaming the Upper Rock were actually monkeys. How did they get there? Nobody knew. Most of the surnames of the locals were neither British nor Spanish, but Italian – after Britain seized the Rock, hordes of sailors and merchants streamed in from Genoa to service the garrison's needs. Even the Rock itself, that great symbol of solidity and strength seemed to defy expectations. Its Roman name is Mons Calpe, Hollow Mountain, due to the caverns that honeycomb its limestone, many of them so deep that they were thought to mark the entrance to hell. These are now interlinked by 33 miles of hand-bored tunnels – leaving more road inside the Rock than out. The impregnable fortress has a hollow centre.

Between interviews, I sat in the Alameda botanical gardens. Somewhere as full of contradictions as Gibraltar would be a troubling place to practise as a lawyer, I decided. But as a setting for a fictional lawyer … I saw a small bronze statue standing by the park bench. A young woman in a wide-brimmed hat. I checked the plinth: Molly Bloom. The creation of an author who never set foot in Gibraltar now has a permanent berth. I took out my notebook, nodded to the ghosts of Defoe, Coleridge, Thackeray et al, and started writing.