Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


#pragma const_seg( [ [ { push | pop}, ] [ identifier, ] ] [ "segment-name" [, "segment-class" ] )

Specifies the segment where const variables are stored in the .obj file. OBJ files can be viewed with the dumpbin application. The default segment in the .obj file for const variables is .rdata. Some const variables, such as scalars, are automatically inlined into the code stream. Inlined code will not appear in .rdata.

data_seg with no parameters resets the segment to .rdata.

push (optional)
Puts a record on the internal compiler stack. A push can have an identifier and segment-name.
pop (optional)
Removes a record from the top of the internal compiler stack.
identifier (optional)
When used with push, assigns a name to the record on the internal compiler stack. When used with pop, pops records off the internal stack until identifier is removed; if identifier is not found on the internal stack, nothing is popped.

Using identifier enables multiple records to be popped with a single pop command.

"segment-name" (optional)
The name of a segment. When used with pop, the stack is popped and segment-name becomes the active segment name.
"segment-class" (optional)
Included for compatibility with C++ prior to version 2.0. It is ignored.


// pragma_directive_const_seg.cpp
// compile with: /GX
#include <iostream>

const int i = 7;               // inlined, not stored in .rdata
const char sz1[]= "test1";      // stored in .rdata

#pragma const_seg(".my_data1")
const char sz2[]= "test2";      // stored in .my_data1

#pragma const_seg(push, stack1, ".my_data2")
const char sz3[]= "test3";      // stored in .my_data2

#pragma const_seg(pop, stack1)   // pop stack1 from stack
const char sz4[]= "test4";      // stored in .my_data1

int main() {
   // const data must be referenced to be put in .obj
   std::cout << sz1 << std::endl;
   std::cout << sz2 << std::endl;
   std::cout << sz3 << std::endl;
   std::cout << sz4 << std::endl;



See /SECTION for a list of names you should not use when creating a section.

You can also specify sections for initialized data (data_seg), uninitialized data (bss_seg), and functions (code_seg).

See Also

Pragma Directives

© 2016 Microsoft