1

In GCC compiler We can use the following code to simulate a constructor:

    __attribute((constructor)) void ctor(){
        printf("hello, ctor.\n");
    }
    int main(int, char** argv){
        printf("hello, main.\n");
    }

In Linux using gcc, the result is:

    hello, ctor.        
    hello, main.

The technology is specific to gcc compiler, does MSVC provide the similar functionality?

Thanks a lot!

buf1024
  • 31
  • 6
  • 2
    No Google, no cry? http://google.com/search?q=msvc+attribute+constructor –  Jan 11 '14 at 09:10
  • @H2CO3 What's the point of this? A portable way to achieve the same would be to use a global `static int dummy = myconstructorfunc();` which would also be called before main. Or does this achieve something different? – Devolus Jan 11 '14 at 09:17
  • @Devolus IDK, ask OP. I rarely use compiler-specific features. –  Jan 11 '14 at 09:22
  • good, I use VC + c + conductor to as google, but find nothing useful! You help me a lot! – buf1024 Jan 11 '14 at 09:53
  • I just tested this, apparently it works only when compiled as C++. After compiling it as C, I also get an error, so you probably have to use this compiler specific feature nevertheless. – Devolus Jan 11 '14 at 10:27

0 Answers0