4.18 location Pragma

The IAR location pragma places the following static storage duration object at the specified address or in the named section, based on the pragma argument.

Suggested Replacement

There is no equivalent MPLAB XC8 pragma, but there are two specifiers that perform similar tasks.

Use the __at(address) specifier to have an object placed at a specific address.

The MPLAB XC8 section attribute can be used with objects to specify the section in which it is placed. If the Common C Interface is enabled, you can instead use the __section specifier.

Caveats

The Common C Interface must be enabled using -mext=cci and <xc.h> must be included into your code to use the __at() specifier.

The sections generated by the section attribute are not concatenated across translation units by the linker, and the -Wl,--section-start,section_name=address option, only works with custom (non-standard) sections.

Examples

Consider migrating IAR code such as:
#pragma location=0x200
__no_init volatile int myVar;   /* myVar is located at address 0x200 */

#pragma segment="FLASH"
#pragma location="FLASH"
__no_init char arr2[];           /* arr2 is located in segment FLASH */

int main(void) {
    myVar = arr[myVar] + arr2[myVar];
}
to MPLAB XC8 code similar to:
#include <xc.h>
 
const char arr[] __at(0x500) = "abc";  /* Place at address 0x500 in program memory */
volatile int myVar __at(0x800200) = 3; /* Place at address 0x200 in data memory */
 
const char arr2[] __section("FLASH") = "def"; /* Placed in named section mysec */
int main(void) {
    myVar = arr[myVar] + arr2[myVar];
}
and build with the -mext=cci option.

Further Information

See the Absolute Variables, Attributes and Ext Option sections in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on these specifiers, attribute, and option.