0

I have a VC project with hundreds of global variables and buffers placed into several source files. I want the linker to put these variables physically adjacent in the final output executable in order that I can easily clean up these variables. I know that gcc linker can do that via a linker script. I just wonder if vc linker can also do that.

Thanks

Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34

1 Answers1

1

You can use

#pragma section( "section-name" [, attributes] )

to declare a new section. Then use

__declspec(allocate("segname")) declarator

to specify in which section your global variable is placed.

I believe this is what GCC does, though haven't experimented myself in VC.

Below are MS links:

section

allocate

There is also another similar question on stackoverflow:

How to place a variable at a given absolute address in memory (with Visual C++)

Community
  • 1
  • 1
Guibao Wang
  • 405
  • 4
  • 15
  • This method is a little troublesome since I have to put the __declspec to all the hundreds of variables. For gcc linker script, it is to easy to do that, shown as below: .bss { __my_bss_section_start = . .bss(a.o) .sbss(a.o) .bss(b.o) .sbss(b.o) .bss(c.o) .sbss(c.o) __my_bss_section_end = . – Pan Ruochen Jun 17 '13 at 08:58