It depends what you mean by "gun".
Getting to the moon is, at the level of napkin math, basically reaching Earth's escape velocity of 11 km/s. Reaching the moon requires a bit less, both because the moon is in orbit around the Earth (and hasn't itself escaped) and because the moon's gravity provides a slight help.
11 km/s has two big problems. The first is getting to that speed. A "gun" means that once you are out of the barrel, you stop acceleration.
If you want a human to survive, you are going to have to bound acceleration. At 10 Gs that requires about 110 seconds, and a "gun-barrel" of 600 km.
If you up that to 100 Gs, that is 11 seconds. Doing the 1/2 at2 dance we get a barrel of 60 km. But you aren't shooting humans now.
At 1000 Gs you are down to a 6 km barrel.
Already we don't look much like a gun.
I guess we could hit 50,000 Gs, where we get a 120 m long barrel. That is starting to look a bit more gun-like. (50,000 Gs is in the ballpark of what a bullet gets for its few-cm long journey.)
The second problem is the atmosphere. If your gun opens up into the air anywhere near sea level, there is a LOT of air in the way. A pretty good approximation of the amount of air between you and space is air pressure.
100 kPa times 1 meter squared divided by gravity means that, on a strait up-and-down direction, there is 10,000 kg of air above you.
At and above 11 km/s, you'll be converting the air you run into into supersonic plasma. Doing that to 10,000 kg of air is going to take a lot of energy, and will slow you down a lot.
So, to be practical, your gun muzzle has to be really, really high up.
At the top of Mt Everest, pressure is 1/3 as high, which means "only" 3,000 kg of air to get through before you reach space.
Even that is insane.
So your gun will have to be free-standing. Now, building a structure capable of free standing higher than Mt Everest runs into engineering problems. There really aren't any materials with the compressive strength to manage this. You end up building a mountain the size of a continent before you get much higher than Everest.
Luckily people have been thinking about this a lot. The two easiest solutions are to create a dynamic structure, or use tension.
For tension, you have to suspend the gun from above. This seems difficult, and you still need really exotic materials to have the tensile strength required. In this case, you'd stick some object in geostationary orbit, dangle a weight further out and use that as a counter-weight to provide force, drop ridiculously strong cables down into the atmosphere, and use that to suspend your gun barrel.
Of course, at this point you should just crawl up the cables instead, climb past geostationary orbit, do some math, and let go of the counterweight cable when you'd hit the moon. This is the "space beanstalk" accidentally invented to make the gun work.
Another option is a dynamic structure. Here the gun is supported by, well, shooting it from the bottom. Your barrel, floating in the sky, has a series of vacuum-tube towers supporting it. These towers are in turn supported by magnetic levitation; in particular, a magnetic accelerator on the surface shoots magnetic iron up the tube. In the tube, it decelerates the iron, providing an upward lifting force. When the iron reaches the top, it falls down another tube back to the surface, and loops.
If the power ever fails, the entire structure falls to Earth.
Anyhow, this technique can, in theory, support a structure 60+ km long that stretches up to the edge of space. The entire system requires insane amounts of power and has huge numbers of magnetic accelerators dynamically keeping the entire thing from crashing down.
Yet another option would be to float the structure, or drop it and have it fire while falling. A huge dirigible only reaches 2000 m up, not as good as Everest. But you could imagine building a rocket that lifts the gun itself to the edge of space? Would that count?
Now, all of this becomes exponentially easier if your goal isn't to "just shoot". If your payload is in turn a rocket, you can just use the gun to help the rocket get up to speed (to a greater or lesser degree).
Similarly, you can put the gun on a rocket, then shoot the gun high up.