This is because in -O0 call is compiled into the htons function, and your declaration for this function is inside namespace net . In the optimized version of -O2 , for example, the call is replaced by a macro.
You can verify this by precompiling your program using gcc -O0 -E v / s gcc -O2 -E
When using htons
In -O2 , htons translates to
int main() { (__extension__ ( { register unsigned short int __v, __x = (unsigned short int) (1024); if (__builtin_constant_p (__x)) __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc"); __v; } )); }
Generating code will not result in a permission error.
error: 'htons has not been declared in this area
When using net :: htons
When replacing ntohs with net::ntohs , ntohs define is used / displayed for optimization, and your pre-processed code looks like:
int main() { net::(__extension__ ({... ...})); }
And therefore a mistake
error: expected unqualified-id before '(token
Why is this happening
htons can be implemented as a function or macro. If it is defined as a macro, htons will work fine. But if it is defined as a net::htons , it will work fine.
It displays in -O1 or higher, header files display macro versions instead of functions.
Possible solutions
using namespace net;
#ifndef htons
extern "C" { // Add all declarations in global space
Mohit jain
source share