including -Wall -ansi causes bunch of errors

Can someone help me why when I compile using -Wall and -ansi gives the following result, how do I remove them?

Why does it tell me I have an implicit declaration of getopt? getopt is from the library
client.c: In function âmainâ:
client.c:28: warning: implicit declaration of function âgetoptâ
client.c:46: error: âoptargâ undeclared (first use in this function)
client.c:46: error: (Each undeclared identifier is reported only once
client.c:46: error: for each function it appears in.)
client.c:53: error: âoptoptâ undeclared (first use in this function)
client.c:64: error: âoptindâ undeclared (first use in this function)
client.c:68: error: expected expression before â/â token
client.c:67: warning: unused variable âbufâ
client.c:82: error: expected expression before â/â token
client.c:85: warning: implicit declaration of function âbcopyâ
client.c:85: error: âstruct hostentâ has no member named âh_addrâ
client.c:88: error: expected expression before â/â token
client.c:104: error: expected expression before â/â token
client.c:117: error: âelseâ without a previous âifâ
client.c:130: warning: pointer targets in passing argument 6 of ârecvfromâ differ in signedness
client.c:148: warning: pointer targets in passing argument 6 of ârecvfromâ differ in signedness
make: *** [client.o] Error 1

Open in new window

kuntilanakAsked:
Who is Participating?
 
sunnycoderCommented:
client.c:28: warning: implicit declaration of function âgetoptâ
client.c:46: error: âoptargâ undeclared (first use in this function)
client.c:46: error: (Each undeclared identifier is reported only once
client.c:46: error: for each function it appears in.)
client.c:53: error: âoptoptâ undeclared (first use in this function)
client.c:64: error: âoptindâ undeclared (first use in this function)

#include <unistd.h>


client.c:68: error: expected expression before â/â token
show code

client.c:67: warning: unused variable âbufâ

remove buf variable


client.c:82: error: expected expression before â/â token
show code

client.c:85: warning: implicit declaration of function âbcopyâ

add declaration for bcopy


client.c:85: error: âstruct hostentâ has no member named âh_addrâ

This is not a warning, this is an error and a rather strange one because struct hostent does have an h_addr .. show some code


client.c:88: error: expected expression before â/â token
client.c:104: error: expected expression before â/â token
show code

client.c:117: error: âelseâ without a previous âifâ
else if mismatch

client.c:130: warning: pointer targets in passing argument 6 of ârecvfromâ differ in signedness
client.c:148: warning: pointer targets in passing argument 6 of ârecvfromâ differ in signedness
unsigned * Vs signed * ... either fix declaration or cast them



Errors often have cascading effect ... start fixing them in order and some of them would vanish on their own
0
 
kuntilanakAuthor Commented:
if I remove the -Wall and -ansi everything works well
0
 
kuntilanakAuthor Commented:
I've attached the code
code.txt
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
sunnycoderCommented:
>if I remove the -Wall and -ansi everything works well
Makes sense - you are seeing additional warnings because you asked compiler to show all warnings -Wall means show all warnings. You also asked the compiler to ensure that your code is ansi c compliant - hence the errors ...

make the changes I suggested and post back if you still see those errors. Else, you can do away with those two flags and continue development.
0
 
kuntilanakAuthor Commented:
for this part:

client.c:117: error: âelseâ without a previous âifâ
else if mismatch

I don't see any else mismatch, the else has an if right above it
0
 
sunnycoderCommented:
#include <getopts.h>
#include <strings.h>

change all // comments to /**/ comments - former is C++ style

move declaration of buf[256] to beginning of function. C requires declarations to precede usage.

cast the arguments to get rid of last two warnings.
0
 
kuntilanakAuthor Commented:
what should I cast it to?
0
 
sunnycoderCommented:
socklen_t *
0
 
fridomCEO/ProgrammerCommented:
// is find in Ansi-C 99 the problem  is not Wall but -ansi. So gnu extensions are switched of, as you can see about getopt which is not ANSI C Standard. What you may try is adding Posix to the whole stuff and this should cover quite a lot ot extra functions.

Regards
Friedrich
0
 
kuntilanakAuthor Commented:
what do you mean by posix?
0
 
fridomCEO/ProgrammerCommented:
POSIX is another gcc supported standard which mostly holds on Unices. And getopt is part of it. If you specify -ansi than only ANSI C is allowed, (you better include -std=c99 or whatever standard you like to program to properly, I'd argue that -ansi is not a good option on Unices but it' sof course ok if you really just want Standard C, but using getopt clearly tells that one does not really wants to use ANSI C

Regards
Friedrich
0
 
kuntilanakAuthor Commented:
ok... so instead of -ansi what standard should I use? what does the c99 basically mean?
0
 
kuntilanakAuthor Commented:
and by the way changing it to -std=c99 still gives me those getopt errors
0
 
fridomCEO/ProgrammerCommented:
-std=c99 activates the most  actual  standard.  Of course it still does not work for getopt because that is POSIX. But why not simply use -Wall -std=c99 that should do fine, what you must  not use is -ansi which makes the compiler "strict".

Here's the  man pagea about the -std option:

 -std=
           Determine the language standard.   This option is currently only
           supported when compiling C or C++.

           The compiler can accept several base standards, such as c89 or
           c++98, and GNU dialects of those standards, such as gnu89 or
           gnu++98.  By specifing a base standard, the compiler will accept
           all programs following that standard and those using GNU extensions
           that do not contradict it.  For example, -std=c89 turns off certain
           features of GCC that are incompatible with ISO C90, such as the
           "asm" and "typeof" keywords, but not other GNU extensions that do
           not have a meaning in ISO C90, such as omitting the middle term of
           a "?:" expression. On the other hand, by specifing a GNU dialect of
           a standard, all features the compiler support are enabled, even
           when those features change the meaning of the base standard and
           some strict-conforming programs may be rejected.  The particular
           standard is used by -pedantic to identify which features are GNU
           extensions given that version of the standard. For example
           -std=gnu89 -pedantic would warn about C++ style // comments, while
           -std=gnu99 -pedantic would not.

           A value for this option must be provided; possible values are

           c89
           iso9899:1990
               Support all ISO C90 programs (certain GNU extensions that
               conflict with ISO C90 are disabled). Same as -ansi for C code.

           iso9899:199409
               ISO C90 as modified in amendment 1.

           c99
           c9x
           iso9899:1999
           iso9899:199x
               ISO C99.  Note that this standard is not yet fully supported;
               see <http://gcc.gnu.org/gcc-4.3/c99status.html> for more
               information.  The names c9x and iso9899:199x are deprecated.

           gnu89
               GNU dialect of ISO C90 (including some C99 features). This is
               the default for C code.

           gnu99
           gnu9x
               GNU dialect of ISO C99.  When ISO C99 is fully implemented in
               GCC, this will become the default.  The name gnu9x is
               deprecated.

           c++98
               The 1998 ISO C++ standard plus amendments. Same as -ansi for
               C++ code.

           gnu++98
               GNU dialect of -std=c++98.  This is the default for C++ code.

           c++0x
               The working draft of the upcoming ISO C++0x standard. This
               option enables experimental features that are likely to be
               included in C++0x. The working draft is constantly changing,
               and any feature that is enabled by this flag may be removed
               from future versions of GCC if it is not part of the C++0x
               standard.

           gnu++0x
               GNU dialect of -std=c++0x. This option enables experimental
               features that may be removed in future versions of GCC.

You better also  check the unistd.h file ther you'll see the extra switches needed
There one can read:

/* Get values of POSIX options:

   If these symbols are defined, the corresponding features are
   always available.  If not, they may be available sometimes.
   The current values can be obtained with `sysconf'.

   _POSIX_JOB_CONTROL            Job control is supported.
   _POSIX_SAVED_IDS            Processes have a saved set-user-ID
                        and a saved set-group-ID.
   _POSIX_REALTIME_SIGNALS      Real-time, queued signals are supported.
   _POSIX_PRIORITY_SCHEDULING      Priority scheduling is supported.
   _POSIX_TIMERS            POSIX.4 clocks and timers are supported.
   _POSIX_ASYNCHRONOUS_IO      Asynchronous I/O is supported.
   _POSIX_PRIORITIZED_IO      Prioritized asynchronous I/O is supported.
   _POSIX_SYNCHRONIZED_IO      Synchronizing file data is supported.
   _POSIX_FSYNC                  The fsync function is present.
   _POSIX_MAPPED_FILES            Mapping of files to memory is supported.
   _POSIX_MEMLOCK            Locking of all memory is supported.
   _POSIX_MEMLOCK_RANGE            Locking of ranges of memory is supported.
   _POSIX_MEMORY_PROTECTION      Setting of memory protections is supported.
   _POSIX_MESSAGE_PASSING      POSIX.4 message queues are supported.
   _POSIX_SEMAPHORES            POSIX.4 counting semaphores are supported.
   _POSIX_SHARED_MEMORY_OBJECTS      POSIX.4 shared memory objects are supported.
   _POSIX_THREADS            POSIX.1c pthreads are supported.
   _POSIX_THREAD_ATTR_STACKADDR      Thread stack address attribute option supported.
   _POSIX_THREAD_ATTR_STACKSIZE      Thread stack size attribute option supported.
   _POSIX_THREAD_SAFE_FUNCTIONS      Thread-safe functions are supported.
   _POSIX_THREAD_PRIORITY_SCHEDULING
                        POSIX.1c thread execution scheduling supported.
   _POSIX_THREAD_PRIO_INHERIT      Thread priority inheritance option supported.
   _POSIX_THREAD_PRIO_PROTECT      Thread priority protection option supported.
   _POSIX_THREAD_PROCESS_SHARED      Process-shared synchronization supported.
   _POSIX_PII                  Protocol-independent interfaces are supported.
   _POSIX_PII_XTI            XTI protocol-indep. interfaces are supported.
   _POSIX_PII_SOCKET            Socket protocol-indep. interfaces are supported.
   _POSIX_PII_INTERNET            Internet family of protocols supported.
   _POSIX_PII_INTERNET_STREAM      Connection-mode Internet protocol supported.
   _POSIX_PII_INTERNET_DGRAM      Connectionless Internet protocol supported.
   _POSIX_PII_OSI            ISO/OSI family of protocols supported.
   _POSIX_PII_OSI_COTS            Connection-mode ISO/OSI service supported.
   _POSIX_PII_OSI_CLTS            Connectionless ISO/OSI service supported.
   _POSIX_POLL                  Implementation supports `poll' function.
   _POSIX_SELECT            Implementation supports `select' and `pselect'.

So that are tons of oiptoins you can use
If you look in that header for getopt you'll find:
#ifdef      __USE_POSIX2
/* Get definitions and prototypes for functions to process the
   arguments in ARGV (ARGC of them, minus the program name) for
   options given in OPTS.  */
# define __need_getopt
# include <getopt.h>
#endi

So
gcc -Walll -D__USE_POSIX2 -std=c99 or so will probably work. Or you just drop the -std=c99 stuff and just use gcc as is.


Regards
Friedrich
0
 
kuntilanakAuthor Commented:
I tried -Wall -std=c99 and still the same thing
0
 
sunnycoderCommented:
did you make the changes I suggested? http:#23678257   I have compiled the code without any warnings
0
 
kuntilanakAuthor Commented:
yes I have.... the only error left now is the getopt and related to it
0
 
sunnycoderCommented:
#include <getopt.h>
0
 
kuntilanakAuthor Commented:
okay, that works.. 3 errors left:

client.c:84: warning: implicit declaration of function âbzeroâ
client.c:86: warning: implicit declaration of function âbcopyâ
client.c:86: error: âstruct hostentâ has no member named âh_addrâ
0
 
kuntilanakAuthor Commented:
I mean one more:

client.c: In function âmainâ:
client.c:86: error: âstruct hostentâ has no member named âh_addrâ
0
 
sunnycoderCommented:
#include <strings.h>

0
 
kuntilanakAuthor Commented:
already did that, and still it says :

client.c:86: error: âstruct hostentâ has no member named âh_addrâ
0
 
sunnycoderCommented:
looks like your installation is not backward compatible

#define h_addr h_addr_list[0]
0
 
kuntilanakAuthor Commented:
may I know why it also works when I don't have #define h_addr h_addr_list[0]
0
 
sunnycoderCommented:
both are same ... h_addr refers to first element of the list ... I guess the definition is embedded in a condition which is controlled by one of the flags  ... check your sys/socket or netdb header file
0
 
kuntilanakAuthor Commented:
so either if I include it or not it will still work but compiling it with -ansi gives the warning
0
 
sunnycoderCommented:
You would have to include the header files for definition of struct hostent anyway. If you do not #include them, code would not compile.
0
 
kuntilanakAuthor Commented:
what should I include then? I think I've included everything needed
0
 
sunnycoderCommented:
You do not need to include anything else

either use
hp->h_addr_list[0];

instead of
hp->h_addr;

or near top of your file add a line
#define h_addr h_addr_list[0]
0
 
kuntilanakAuthor Commented:
ok, gotcha! thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.