Regarding Attributes

This is a discussion on Regarding Attributes within the C Programming forums, part of the General Programming Boards category; Dear All, What is the wrong with my code... Code: #include "stdio.h" int i=0; void init()_attribute_((constructor)); void deinit()_attribute_((destructor)); void init() ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Regarding Attributes

    Dear All,

    What is the wrong with my code...
    Code:
    #include "stdio.h"
    int i=0;
    void init()_attribute_((constructor));
    void deinit()_attribute_((destructor));
    void init()
    {
    i=10;
    printf("init:i=%d", i);
    }
    
    void deinit()
    {
    i=0;
    printf("deinit:i=%d\n",i);
    return 0;
    }
    
    int main()
    {
     i=i*2;
     printf("main i=%d\n", i);
     return 0;
     }
    i am getting two error as follows

    \Program Files\Microsoft Visual Studio\MyProjects\eewrew\eewrew.cpp(7) : error C2146: syntax error : missing ';' before identifier '__attribute__'
    C:\Program Files\Microsoft Visual Studio\MyProjects\eewrew\eewrew.cpp(7) : fatal error C1004: unexpected end of file found

    Thanks for your help

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    This "#include "stdio.h""
    This "void init()_attribute_((constructor));"
    and this "void deinit()_attribute_((destructor));"

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    how come these are errors?

    Quote Originally Posted by 39ster View Post
    This "#include "stdio.h""
    This "void init()_attribute_((constructor));"
    and this "void deinit()_attribute_((destructor));"
    Hello could you explain me better way..i don't find any errors in these declarations...
    you mean attrubutes needed header file separately....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > void init()_attribute_((constructor));

    1. __attribute__ is a GCC specific extension. It simply WILL NOT WORK with a microsoft compiler.

    2. You're using a C++ compiler to compile your C code. Rename your source files to have a .c extension.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by Salem View Post
    > void init()_attribute_((constructor));

    1. __attribute__ is a GCC specific extension. It simply WILL NOT WORK with a microsoft compiler.

    2. You're using a C++ compiler to compile your C code. Rename your source files to have a .c extension.
    Thanks for your reply

    Is there any way to achieve same thing in VC++ compiler?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    http://msdn.microsoft.com/en-us/libr...05(VS.80).aspx
    #pragma(pack)

    Though compared to the range of features offered by gcc attributes, it's a pretty weak set.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by shwetha_siddu View Post
    Hello could you explain me better way..i don't find any errors in these declarations...
    you mean attrubutes needed header file separately....
    Well i don't know what attributes are but im pretty sure they're not standard C. You're also meant to include stdio.h like this #include <stdio.h> not #include "stdio.h"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting file attributes through a C program
    By rohan_ak1 in forum C Programming
    Replies: 1
    Last Post: 05-23-2008, 08:39 AM
  2. Unable to List Files Attributes on EXT2
    By Maragato in forum C Programming
    Replies: 16
    Last Post: 08-11-2004, 12:30 PM
  3. Access token privilege attributes
    By bennyandthejets in forum Windows Programming
    Replies: 1
    Last Post: 07-10-2003, 11:39 AM
  4. Changing file attributes?
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 01-24-2002, 05:32 AM
  5. Changing file attributes?
    By kes103 in forum C Programming
    Replies: 3
    Last Post: 01-21-2002, 09:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21