specifying data segment

Started by PVozenilek, December 08, 2016, 01:52:22 PM

Previous topic - Next topic

PVozenilek

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.

TimoVJL

#1
PellesC is a c99/c11 C compiler.
#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:initialize
main
finalize
finalize
Press any key to continue...

May the source be with you

PVozenilek

To answer my second question, about __pragma equivalent: there's C99 _Pragma("..") in Pelles C. Unfortunately it is buggy.