If you want to use a development tool such as the MPLAB® REAL ICE™ In-circuit emulator or ICD as a debugger, two things need to happen. First, your compiler needs to be told to reserve the device resources used by the debug executive (code which will execute alongside your program and which will communicate with the debugger), and second, the debug executive needs to be enabled.
When you start a debug session in MPLAB X IDE, as described in "Starting and Stopping A Debug Session", it does both of these things. The IDE passes the appropriate options to the compiler so that it will reserve the required memory locations, and it will also program the appropriate configuration bit in the device to enable background debugging. Neither of these things happen for production builds.
Some devices allow you to program the background debug configuration bit in your program. If so, the config pragma setting might be called DEBUG or BKBUG. However, even if this setting is defined, you should not manually set or clear this bit in your code, even though you typically should specify the settings for all the other configuration bits. Having the IDE control this bit means that you do not need to manually change it as you perform different builds, and it can't be accidentally left in the wrong state.
The bits you might need to program if you are using a 16- or 32-bit PIC device are the JTAG enable bit (called JTAGEN), which turns on debugging facilities that use the JTAG standard, and the communication channel select bits (called either ICS or ICESEL) which specify the device channel (hence the device pins) used by an in-circuit debugger to exchange information between the debugger and the device.
See your device datasheet for more detailed information regarding the purpose of each configuration bit, and open the relevant device HTML file to determine the configuration bit names and allowable values that you can use in your program. These files are called pic_chipinfo.html, pic18_chipinfo.html, config_index.html, and PIC32ConfigSet.html, all in your compiler’s docs directory. Select your target device from those shown when you open the file in your favourite web browser.