NO

Author Topic: specifying data segment  (Read 3303 times)

Offline PVozenilek

  • Member
  • *
  • Posts: 3
specifying data segment
« on: December 08, 2016, 01:52:22 PM »
MSVC has ability to specify PE segment for data (using #pragma(section) and __declspec(allocate)). This allows to write functions which are automatically invoked before main() starts. Detailed description is http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc (and it works for me, with a small fix). I use it to implement easy to use C tests. C++ uses this mechanism to invoke constructors for global objects.

My questions:

1. Is there similar capability in Pelles C?

2. Is there a way to have pragmas as part of macros? MSVC has __pragma for this purpose.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2112
Re: specifying data segment
« Reply #1 on: December 08, 2016, 05:28:07 PM »
PellesC is a c99/c11 C compiler.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

static void initialize(void);
static void finalize(void);
#pragma startup initialize()
#pragma exit finalize()

static void finalize(void)
{
    printf( "finalize\n");
}

static void initialize(void)
{
    printf( "initialize\n");
    atexit( finalize);
}

int main( int argc, char** argv)
{
    printf( "main\n");
    return 0;
}
output:
Code: [Select]
initialize
main
finalize
finalize
Press any key to continue...
« Last Edit: December 08, 2016, 06:40:32 PM by TimoVJL »
May the source be with you

Offline PVozenilek

  • Member
  • *
  • Posts: 3
Re: specifying data segment
« Reply #2 on: February 11, 2017, 04:47:34 PM »
To answer my second question, about
Code: [Select]
__pragma equivalent: there's C99
Code: [Select]
_Pragma("..") in Pelles C. Unfortunately it is buggy.