Compilation error with -O2 option in g++

This is a discussion on Compilation error with -O2 option in g++ within the C++ Programming forums, part of the General Programming Boards category; Please suggest how can I get rid of error in following code -------klip--------------------- Code: #include <iostream> #include <map> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Compilation error with -O2 option in g++

    Please suggest how can I get rid of error in following code

    -------klip---------------------
    Code:
    #include <iostream>
    #include <map>
    #include <time.h>
    #include <netinet/in.h> // For sockaddr_in
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    using namespace std;
    
    #define VAL() htonl(0x00FFFFFF)
    const uint32_t AVP_LENGTH_VALUE = VAL();
    
    int main()
    {
    }
    
    -------klap--------------------
    
    Compilation
    ----------klip------------------
    $g++ -v  -O2  test.cxx
    Using built-in specs.
    Target: x86_64-redhat-linux
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
    Thread model: posix
    gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
     /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/cc1plus -quiet -v -D_GNU_SOURCE test.cxx -quiet -dumpbase test.cxx -mtune=generic -auxbase test -O2 -version -o /tmp/cclvQAdT.s
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../x86_64-redhat-linux/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/x86_64-redhat-linux
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward
     /usr/local/include
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
     /usr/include
    End of search list.
    GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-48) (x86_64-redhat-linux)
            compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-48).
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: ad35c03ce9997656f54e642ebd49f167
    test.cxx:13: error: statement-expressions are allowed only inside functions
    test.cxx:13: error: register name not specified for __v
    test.cxx:13: error: expected `)' before : token

    ----------klap-----------------
    Last edited by Salem; 10-20-2010 at 07:03 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,264
    Don't initialise a const variable at file scope using the return value of a function call - you need to initialise it with a value that can be computed at compile time (which htonl(0x00FFFFFF) cannot).

    The (first) error message is actually telling you that.

    It's got nothing to do with compiler optimisation settings.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    It has something to do with compiler optimization, because if I omit -O2 from compilation, its through.

    Even I tried with -O and -O1 same result. In older version of g++ 3.* I did not face this problem.

    I tried removing the "const" qualifier, no help.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    ok .. I tried different l ways .Finally, I made the global declaration #define as follows

    Code:
    #define AVP_LENGTH_VALUE htonl(0x00FFFFFF)

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    It's a bug in GCC. Upgrade if you can - 2 years old is forever in compiler terms.

    If you can't, it looks like removing "const" from the declaration should fix it. Or use the #define like you mention.

    My guess is htonl is #defined using some sort of anonymous struct or union which seems to be what triggers the bug.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    It's not a bug!
    The return value of a function is not a constant expression, thus you cannot initialize const variables with function return values! If you make it non-const, it works as expected, however, because it's perfectly legal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Favourite program
    By progcomputeach in forum General Discussions
    Replies: 18
    Last Post: 08-04-2010, 05:27 AM
  2. fred fwrite
    By alexrelph in forum C Programming
    Replies: 2
    Last Post: 03-07-2010, 04:45 PM
  3. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  4. Problem with compiling code with option -O2
    By koushikyou in forum C Programming
    Replies: 16
    Last Post: 01-07-2009, 05:03 AM
  5. Replies: 3
    Last Post: 12-06-2008, 06:54 PM

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