#include errors

Discussion in 'C Programming' started by zolli, Aug 23, 2005.

  1. zolli

    zolli Guest

    Hi,

    I've been banging my head against this for a while now. Hoping someone
    here can shed some light on what's going on.

    On including stdlib.h in a file, I'm seeing the following errors:

    ----BEGIN ERRORS----
    gcc -g3 -DUSE_LIBC -Wall -c -I../mm -I../include -I/usr/include
    -I/usr/include/linux -o mm_tree_test.o mm_tree_test.c
    In file included from mm_tree_test.c:2:
    /usr/include/stdlib.h:137: error: syntax error before
    "__ctype_get_mb_cur_max"
    In file included from /usr/include/sys/types.h:266,
    from /usr/include/stdlib.h:416,
    from mm_tree_test.c:2:
    /usr/include/bits/pthreadtypes.h:50: error: syntax error before "size_t"
    /usr/include/bits/pthreadtypes.h:53: error: syntax error before
    "__stacksize"
    In file included from mm_tree_test.c:2:
    /usr/include/stdlib.h:433: error: syntax error before "size_t"
    /usr/include/stdlib.h:462: error: syntax error before "size_t"
    /usr/include/stdlib.h:556: error: syntax error before "__size"
    /usr/include/stdlib.h:558: error: syntax error before "__nmemb"
    /usr/include/stdlib.h:567: error: syntax error before "size_t"
    In file included from /usr/include/stdlib.h:578,
    from mm_tree_test.c:2:
    /usr/include/alloca.h:33: error: syntax error before "__size"
    In file included from mm_tree_test.c:2:
    /usr/include/stdlib.h:583: error: syntax error before "__size"
    /usr/include/stdlib.h:739: error: syntax error before "size_t"
    /usr/include/stdlib.h:743: error: syntax error before "size_t"
    /usr/include/stdlib.h:812: error: syntax error before "size_t"
    /usr/include/stdlib.h:815: error: syntax error before "size_t"
    /usr/include/stdlib.h:819: error: syntax error before "size_t"
    /usr/include/stdlib.h:822: error: syntax error before "size_t"
    /usr/include/stdlib.h:830: error: syntax error before "size_t"
    /usr/include/stdlib.h:833: error: syntax error before '*' token
    /usr/include/stdlib.h:837: error: syntax error before "wchar_t"
    /usr/include/stdlib.h:841: error: syntax error before "mbstowcs"
    /usr/include/stdlib.h:841: error: syntax error before '*' token
    /usr/include/stdlib.h:844: error: syntax error before "wcstombs"
    /usr/include/stdlib.h:845: error: syntax error before '*' token
    mm_tree_test.c: In function `main':
    ----END ERRORS----

    I've posted all relevant code below. If I remove the line:

    #include <stdlib.h>

    from mm_tree_test.c, the errors go away. Any ideas?

    Thanks in advance.

    zolli


    ----BEGIN mm_tree_test.c----
    #include <mm_tree.h>
    #include <stdlib.h>
    #include <macro.h>

    int main( int argc, char** argv ) {
    /* do nothing */
    return 0;
    }
    ----END mm_tree_test.c----


    ----BEGIN mm_tree.h----
    #ifndef INCLUDE_MM_TREE_H
    #define INCLUDE_MM_TREE_H

    #include <stdtype.h>

    struct mm_avlnode_str
    {
    uint32 val;
    struct mm_avlnode_str *left;
    struct mm_avlnode_str *right;
    int32 height;
    };
    ....(truncated)...
    #endif /* INCLUDE_MM_TREE_H */
    ----END mm_tree.h----


    ----BEGIN mm_tree.c----
    #include "mm_tree.h"
    #include <macro.h>

    /* pre-declarations */
    static mmTreeNode insert( mmTreeNode node, mmTreeNode root );
    static mmTreeNode remove( mmTreeNode node, mmTreeNode root );
    ....(truncated)...

    /* API function implementations defined in .h */
    void mmTree_insert( mmTree t, mmTreeNode node ) {
    t->root = insert( node, t->root );
    }
    void mmTree_remove( mmTree t, mmTreeNode node ) {
    t->root = remove( node, t->root );
    }
    ....(truncated)...
    ----END mm_tree.c----


    ----BEGIN stdtype.h----
    #ifndef INCLUDE_STDTYPE_H
    #define INCLUDE_STDTYPE_H

    typedef enum { FALSE, TRUE } bool;
    #define ARCH_32_BIT 1
    #define PAGE_LEN 0x1000 /* 4K */

    #ifdef ARCH_32_BIT
    #define int32 int
    #define uint32 unsigned int
    #define int16 short int
    #define uint16 unsigned short int
    #define int8 char
    #define uint8 unsigned char
    #define byte unsigned char
    #endif

    #define ulong unsigned long int
    #define uint unsigned int
    #define ushort unsigned short int
    #define uchar unsigned char

    #ifndef NULL
    #define NULL ((void *)0)
    #endif

    #endif /* INCLUDE_STDTYPE_H */
    ----END stdtype.h----


    ----BEGIN macro.h----
    #ifndef INCLUDE_MACRO_H
    #define INCLUDE_MACRO_H

    extern int printf(const char *format, ...);

    #define log_err(format, args...) \
    printf("[ERROR]%s:%d:%s(): " format "\n", \
    __FILE__,__LINE__,__FUNCTION__, ##args)
    #define log_warn(format, args...) \
    printf("[WARN]%s:%d:%s(): " format "\n", \
    __FILE__,__LINE__,__FUNCTION__, ##args)
    #define log_trace(format, args...) \
    printf("[TRACE]%s:%d:%s(): " format "\n", \
    __FILE__,__LINE__,__FUNCTION__, ##args)
    #define log_trace_fxn() \
    printf("[TRACE-FXN]%s:%d:%s(): \n", \
    __FILE__,__LINE__,__FUNCTION__)
    #define log_dbg(format, args...) \
    printf("[DEBUG]%s:%d:%s(): " format "\n", \
    __FILE__,__LINE__,__FUNCTION__, ##args)
    #define log_ver(format, args...) \
    printf("[VERBOSE]%s:%d:%s(): " format "\n", \
    __FILE__,__LINE__,__FUNCTION__, ##args)

    #endif /* INCLUDE_MACRO_H */
    ----END macro.h----
     
    zolli, Aug 23, 2005
    #1
    1. Advertising

  2. zolli

    Richard Bos Guest

    zolli <> wrote:

    > I've been banging my head against this for a while now. Hoping someone
    > here can shed some light on what's going on.
    >
    > On including stdlib.h in a file, I'm seeing the following errors:


    [ Snip syntax errors. ]

    > I've posted all relevant code below. If I remove the line:
    >
    > #include <stdlib.h>
    >
    > from mm_tree_test.c, the errors go away. Any ideas?


    > ----BEGIN mm_tree_test.c----
    > #include <mm_tree.h>
    > #include <stdlib.h>


    Yes. There is probably an error on the last code line of <mm_tree.h>.

    > ----BEGIN mm_tree.h----
    > #ifndef INCLUDE_MM_TREE_H
    > #define INCLUDE_MM_TREE_H
    >
    > #include <stdtype.h>
    >
    > struct mm_avlnode_str
    > {
    > uint32 val;
    > struct mm_avlnode_str *left;
    > struct mm_avlnode_str *right;
    > int32 height;
    > };
    > ...(truncated)...
    > #endif /* INCLUDE_MM_TREE_H */
    > ----END mm_tree.h----


    I.e., the last bit of "(truncated)" probably has a syntax error.

    Richard
     
    Richard Bos, Aug 23, 2005
    #2
    1. Advertising

  3. zolli wrote:
    > I've posted all relevant code below.


    Not true. You have failed to include the definitions for mmTreeNode and
    mmTree.

    > If I remove the line:
    > #include <stdlib.h>
    > from mm_tree_test.c, the errors go away. Any ideas?


    Of course. Clean up the mess, so you don't confuse yourself with your
    multiple inclusions. What happens when you include that code directly?
    The last lines of <mm_tree.h> (do you really mean to use angle
    brackets?) seem to interfere with <stdlib.h>.
    > ----BEGIN mm_tree_test.c----
    > #include <mm_tree.h>
    > #include <stdlib.h>


    There's no way for us to tell, since you have very inconveniently
    replaced the end of mm_tree.h with
    > ...(truncated)...
    > #endif /* INCLUDE_MM_TREE_H */
     
    Martin Ambuhl, Aug 23, 2005
    #3
  4. zolli

    zolli Guest

    Hi,

    Richard Bos wrote:
    > zolli <> wrote:
    >
    >
    >>I've been banging my head against this for a while now. Hoping someone
    >>here can shed some light on what's going on.
    >>
    >>On including stdlib.h in a file, I'm seeing the following errors:

    >
    >
    > [ Snip syntax errors. ]


    Thanks for the replies. I spent some more time on this: stripping down
    everything to the bare minimum, verifying that mm_tree.o compiles just
    fine by itself, running the code through the preprocessor (gcc -E) &
    examining the output. What a pain.

    Turns out if I remove these two includes from the Makefile:

    -I/usr/include -I/usr/include/linux

    then everything works as expected. Not sure why. The system is debian
    3.1; I suspect there's something going on with the way gcc tracks down
    the system includes.

    Thanks again for the help.

    zolli

    >
    >
    >>----BEGIN mm_tree_test.c----
    >>#include <mm_tree.h>
    >>#include <stdlib.h>

    >
    >
    > Yes. There is probably an error on the last code line of <mm_tree.h>.
    >
    >
    >>----BEGIN mm_tree.h----
    >>#ifndef INCLUDE_MM_TREE_H
    >>#define INCLUDE_MM_TREE_H
    >>
    >>#include <stdtype.h>
    >>
    >>struct mm_avlnode_str
    >>{
    >> uint32 val;
    >> struct mm_avlnode_str *left;
    >> struct mm_avlnode_str *right;
    >> int32 height;
    >>};
    >>...(truncated)...
    >>#endif /* INCLUDE_MM_TREE_H */
    >>----END mm_tree.h----

    >
    >
    > I.e., the last bit of "(truncated)" probably has a syntax error.
    >
    > Richard
     
    zolli, Aug 24, 2005
    #4
  5. zolli

    Richard Bos Guest

    zolli <> wrote:

    [ Please don't mail _and_ post follow-ups. ]

    > Richard Bos wrote:
    > > zolli <> wrote:
    > >
    > >>I've been banging my head against this for a while now. Hoping someone
    > >>here can shed some light on what's going on.
    > >>
    > >>On including stdlib.h in a file, I'm seeing the following errors:

    > >
    > > [ Snip syntax errors. ]


    > Turns out if I remove these two includes from the Makefile:
    >
    > -I/usr/include -I/usr/include/linux
    >
    > then everything works as expected. Not sure why.


    I do; you've probably broken your make. Try #including another standard
    header (e.g., <stdio.h> somewhere and see if it works. In any case, I do
    suspect that you've just glossed over the symptoms, and not solved your
    actual problem.

    Richard
     
    Richard Bos, Aug 24, 2005
    #5
  6. zolli

    Villy Kruse Guest

    On 24 Aug 2005 00:56:02 -0500,
    zolli <> wrote:


    >
    > Turns out if I remove these two includes from the Makefile:
    >
    > -I/usr/include -I/usr/include/linux
    >
    > then everything works as expected. Not sure why. The system is debian
    > 3.1; I suspect there's something going on with the way gcc tracks down
    > the system includes.
    >


    That is because the search sequence does make a difference, and /usr/include
    should not be searched before other system and architecture dependent include
    directories. These directories are specified in the compiler set-up, and if
    you specify -I/usr/include you will upset the proper search sequence.

    Specifying -I /usr/include/linux is almost always a mistake. Include files
    from this directory should by included using #include <linux/xxx.h>. That
    being said, user level programs should only in very rare circumstances
    include anything directly from this directory.

    Villy
     
    Villy Kruse, Aug 24, 2005
    #6
  7. zolli

    zolli Guest

    Richard Bos wrote:
    > zolli <> wrote:
    >
    > [ Please don't mail _and_ post follow-ups. ]


    Sorry about that; hit Reply-all instead of Reply.

    >>Richard Bos wrote:
    >>
    >>>zolli <> wrote:
    >>>
    >>>
    >>>>I've been banging my head against this for a while now. Hoping someone
    >>>>here can shed some light on what's going on.
    >>>>
    >>>>On including stdlib.h in a file, I'm seeing the following errors:
    >>>
    >>>[ Snip syntax errors. ]

    >
    >
    >>Turns out if I remove these two includes from the Makefile:
    >>
    >>-I/usr/include -I/usr/include/linux
    >>
    >>then everything works as expected. Not sure why.

    >
    >
    > I do; you've probably broken your make. Try #including another standard
    > header (e.g., <stdio.h> somewhere and see if it works. In any case, I do
    > suspect that you've just glossed over the symptoms, and not solved your
    > actual problem.


    That's quite possible. I'm new to C programming; I'll post the complete
    test code here so you can take a look. If I'm doing something
    fundamentally wrong, I'd like to know. In the Makefile, if I replace
    this line:
    INCLUDES := -I../mm
    with this line:
    INCLUDES := -I../mm -I/usr/include -I/usr/include/linux

    The aforementioned errors are seen.

    The directory structure looks like this:
    /test/Makefile
    /test/mm_tree_test.c
    /mm/mm_tree.h
    /mm/mm_tree.c

    Thanks,
    zolli


    <Makefile>
    all: mm_tree_test

    CC := gcc
    CFLAGS = -g3 -DUSE_LIBC -Wall
    INCLUDES := -I../mm -I../include

    mm_tree_test.o: mm_tree_test.c ../mm/mm_tree.h ../mm/mm_tree.c
    cd ../mm && $(CC) $(CFLAGS) -c $(INCLUDES) -o mm_tree.o mm_tree.c && cd
    .../test
    $(CC) $(CFLAGS) -c $(INCLUDES) -o $@ $<
    mm_tree_test: mm_tree_test.o
    $(CC) $(CFLAGS) -o mm_tree_test mm_tree_test.o ../mm/mm_tree.o
    clean:
    rm *.o
    </Makefile>


    <mm_tree_test.c>
    #include <stdlib.h>
    #include <mm_tree.h>

    int main( int argc, char** argv ) {
    return 0;
    }
    </mm_tree_test.c>


    <mm_tree.h>
    #ifndef INCLUDE_MM_TREE_H
    #define INCLUDE_MM_TREE_H

    #ifndef NULL
    #define NULL ((void *)0)
    #endif

    struct avlnode
    {
    int val;
    struct avlnode *left;
    struct avlnode *right;
    int height;
    };
    struct avltree
    {
    struct avlnode *root;
    };
    int avltree_height( struct avltree *t );

    #endif
    </mm_tree.h>


    <mm_tree.c>
    #include "mm_tree.h"

    static int height( struct avlnode *node );

    int mmTree_height( struct avltree *t ) {
    return height( t->root );
    }
    static int height( struct avlnode *node ) {
    if( node == NULL ) {
    return -1;
    }
    return node->height;
    }
    </mm_tree.c>
     
    zolli, Aug 24, 2005
    #7
  8. zolli

    Flash Gordon Guest

    zolli wrote:
    > Richard Bos wrote:


    <snip>

    > test code here so you can take a look. If I'm doing something
    > fundamentally wrong, I'd like to know. In the Makefile, if I replace
    > this line:
    > INCLUDES := -I../mm
    > with this line:
    > INCLUDES := -I../mm -I/usr/include -I/usr/include/linux
    >
    > The aforementioned errors are seen.


    So don't do that. Nothing in your code requires it and nothing should
    *ever* require adding /usr/include explicitly to the path. Ask in a
    Linux group for details about search paths.

    > The directory structure looks like this:
    > /test/Makefile
    > /test/mm_tree_test.c
    > /mm/mm_tree.h
    > /mm/mm_tree.c
    >
    > Thanks,
    > zolli
    >
    >
    > <Makefile>
    > all: mm_tree_test
    >
    > CC := gcc
    > CFLAGS = -g3 -DUSE_LIBC -Wall


    I would suggest
    CFLAGS = -g3 -O -DUSE_LIBC -Wall -ansi -pedantic
    Possibly even loose the -DUSE_LIBC since I can't see why you are doing it.

    > INCLUDES := -I../mm -I../include


    Just have
    INCLUDES := -I../mm

    > mm_tree_test.o: mm_tree_test.c ../mm/mm_tree.h ../mm/mm_tree.c
    > cd ../mm && $(CC) $(CFLAGS) -c $(INCLUDES) -o mm_tree.o mm_tree.c &&
    > cd ../test
    > $(CC) $(CFLAGS) -c $(INCLUDES) -o $@ $<
    > mm_tree_test: mm_tree_test.o
    > $(CC) $(CFLAGS) -o mm_tree_test mm_tree_test.o ../mm/mm_tree.o
    > clean:
    > rm *.o
    > </Makefile>
    >
    >
    > <mm_tree_test.c>
    > #include <stdlib.h>
    > #include <mm_tree.h>


    The above should be
    #include "mm_tree.h"

    mm_tree.h is yours, not a system header.

    > int main( int argc, char** argv ) {
    > return 0;
    > }
    > </mm_tree_test.c>
    >
    >
    > <mm_tree.h>
    > #ifndef INCLUDE_MM_TREE_H
    > #define INCLUDE_MM_TREE_H
    >
    > #ifndef NULL
    > #define NULL ((void *)0)
    > #endif


    Don't define NULL yourself, just include one of the headers that
    provides it when you need it. Otherwise, if someone includes your header
    then a standard header things might break.

    > struct avlnode
    > {
    > int val;
    > struct avlnode *left;
    > struct avlnode *right;
    > int height;
    > };
    > struct avltree
    > {
    > struct avlnode *root;
    > };
    > int avltree_height( struct avltree *t );
    >
    > #endif
    > </mm_tree.h>
    >
    >
    > <mm_tree.c>
    > #include "mm_tree.h"


    #include <stdlib.h>

    Then you will have a definition of NULL. Alternatively see below...

    > static int height( struct avlnode *node );
    >
    > int mmTree_height( struct avltree *t ) {
    > return height( t->root );
    > }
    > static int height( struct avlnode *node ) {
    > if( node == NULL ) {


    If you don't want to include a system header (although you will probably
    need it for other stuff later) use either
    if( node == 0 ) {
    or
    if( !node ) {

    > return -1;
    > }
    > return node->height;
    > }
    > </mm_tree.c>



    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 24, 2005
    #8
  9. zolli

    CBFalconer Guest

    zolli wrote:
    > Richard Bos wrote:
    >> zolli <> wrote:
    >>
    >> [ Please don't mail _and_ post follow-ups. ]

    >
    > Sorry about that; hit Reply-all instead of Reply.


    Then simply cancel the message with CTRL-W and try again.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Aug 24, 2005
    #9
  10. zolli

    zolli Guest

    Flash Gordon wrote:
    > zolli wrote:
    >
    >> Richard Bos wrote:

    >
    >
    > <snip>
    >
    >> test code here so you can take a look. If I'm doing something
    >> fundamentally wrong, I'd like to know. In the Makefile, if I replace
    >> this line:
    >> INCLUDES := -I../mm
    >> with this line:
    >> INCLUDES := -I../mm -I/usr/include -I/usr/include/linux
    >>
    >> The aforementioned errors are seen.

    >
    >
    > So don't do that. Nothing in your code requires it and nothing should
    > *ever* require adding /usr/include explicitly to the path. Ask in a
    > Linux group for details about search paths.


    Thanks for the advice.

    >> The directory structure looks like this:
    >> /test/Makefile
    >> /test/mm_tree_test.c
    >> /mm/mm_tree.h
    >> /mm/mm_tree.c
    >>
    >> Thanks,
    >> zolli
    >>
    >>
    >> <Makefile>
    >> all: mm_tree_test
    >>
    >> CC := gcc
    >> CFLAGS = -g3 -DUSE_LIBC -Wall

    >
    >
    > I would suggest
    > CFLAGS = -g3 -O -DUSE_LIBC -Wall -ansi -pedantic
    > Possibly even loose the -DUSE_LIBC since I can't see why you are doing it.


    The -DUSE_LIBC is there for the sake of the project. In the actual
    (non-test) source, I compile modules, such as the binary search tree,
    using glibc in order to test them. Later, the code is used in a
    standalone OS which doesn't currently have a libc.

    I've used pedantic, but never tried -ansi; I'll add it to my base
    Makefile now.

    >
    >> INCLUDES := -I../mm -I../include

    >
    >
    > Just have
    > INCLUDES := -I../mm
    >
    >> mm_tree_test.o: mm_tree_test.c ../mm/mm_tree.h ../mm/mm_tree.c
    >> cd ../mm && $(CC) $(CFLAGS) -c $(INCLUDES) -o mm_tree.o mm_tree.c
    >> && cd ../test
    >> $(CC) $(CFLAGS) -c $(INCLUDES) -o $@ $<
    >> mm_tree_test: mm_tree_test.o
    >> $(CC) $(CFLAGS) -o mm_tree_test mm_tree_test.o ../mm/mm_tree.o
    >> clean:
    >> rm *.o
    >> </Makefile>
    >>
    >>
    >> <mm_tree_test.c>
    >> #include <stdlib.h>
    >> #include <mm_tree.h>

    >
    >
    > The above should be
    > #include "mm_tree.h"
    >
    > mm_tree.h is yours, not a system header.


    I read the K&R book a little bit today, trying to figure out when to use
    "include.h" vs. <include.h>. The book was a bit nebulous, but I see
    that the C FAQ has more solid advice that mirrors yours.

    Again, thank you for the helpful suggestions.

    zolli

    >> #ifndef NULL
    >> #define NULL ((void *)0)
    >> #endif

    >
    > Don't define NULL yourself, just include one of the headers that
    > provides it when you need it. Otherwise, if someone includes your header
    > then a standard header things might break.
    >
    >> struct avlnode
    >> {
    >> int val;
    >> struct avlnode *left;
    >> struct avlnode *right;
    >> int height;
    >> };
    >> struct avltree
    >> {
    >> struct avlnode *root;
    >> };
    >> int avltree_height( struct avltree *t );
    >>
    >> #endif
    >> </mm_tree.h>
    >>
    >>
    >> <mm_tree.c>
    >> #include "mm_tree.h"

    >
    >
    > #include <stdlib.h>
    >
    > Then you will have a definition of NULL. Alternatively see below...
    >
    >> static int height( struct avlnode *node );
    >>
    >> int mmTree_height( struct avltree *t ) {
    >> return height( t->root );
    >> }
    >> static int height( struct avlnode *node ) {
    >> if( node == NULL ) {

    >
    >
    > If you don't want to include a system header (although you will probably
    > need it for other stuff later) use either
    > if( node == 0 ) {
    > or
    > if( !node ) {
    >
    >> return -1;
    >> }
    >> return node->height;
    >> }
    >> </mm_tree.c>

    >
    >
    >
     
    zolli, Aug 25, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,035
    Mark Goldin
    Jan 17, 2004
  2. Danny Anderson
    Replies:
    5
    Views:
    531
    Victor Bazarov
    Aug 15, 2003
  3. Rolf Magnus
    Replies:
    2
    Views:
    630
    Karl Heinz Buchegger
    Nov 28, 2003
  4. Elie Nader
    Replies:
    1
    Views:
    666
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    1,014
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page