Beginners question on inline C

This is a discussion on Beginners question on inline C within the C Programming forums, part of the General Programming Boards category; We are trying to improve our varnish reverse-proxy with some inline C. And as such, I have been starting to ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Question Beginners question on inline C

    We are trying to improve our varnish reverse-proxy with some inline C. And as such, I have been starting to dabble. I have put in what I think to be some rather simple code, but get an error which I believe is due to my ubuntu setup.

    The below code works fine:

    C{
    #include <syslog.h>
    #include <string.h>
    }C



    But this returns an error:

    C{
    #include <syslog.h>
    #include <string.h>
    #include <libxml2/libxml/parser.h>
    }C


    storage_file: filename: /var/lib/varnish/retrobadger-desktop/varnish_storage.bin size 1024 MB.
    Message from C-compiler:
    In file included from ./vcl.1P9zoqAU.c:418:
    /usr/include/libxml2/libxml/parser.h:15: fatal error: libxml/xmlversion.h: No such file or directory
    compilation terminated.
    Running C-compiler failed, exit 1
    VCL compilation failed


    Any ideas on whether my code is wrong, the parser.h file appears to be in the correct directory by default, so not sure where to start.

    Thankyou for any advice you can give,
    Dan

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Your include statement should probably look like:
    Code:
    #include <libxml/parser.h>
    Note that I got rid of the libxml2 part of the path. You will need to updated your GCC command to include the proper path. Basically, this means adding a -I/path/to/libxml2 to the command line.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    So it looks like libxml2/libxml/parser.h is correctly included, but libxml/xmlversion.h can't be found. Read up here on #include and search path stuff: Header Files - The C Preprocessor.

    I'm guessing parser.h has a line like '#include <libxml/versioh.h>' in it somewhere. You may have to add a -L/usr/include/libxml2 to your make file or compiler script.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by retrodans View Post
    We are trying to improve our varnish reverse-proxy with some inline C. And as such, I have been starting to dabble. I have put in what I think to be some rather simple code, but get an error which I believe is due to my ubuntu setup.

    The below code works fine:

    C{
    #include <syslog.h>
    #include <string.h>
    }C



    But this returns an error:

    C{
    #include <syslog.h>
    #include <string.h>
    #include <libxml2/libxml/parser.h>
    }C


    storage_file: filename: /var/lib/varnish/retrobadger-desktop/varnish_storage.bin size 1024 MB.
    Message from C-compiler:
    In file included from ./vcl.1P9zoqAU.c:418:
    /usr/include/libxml2/libxml/parser.h:15: fatal error: libxml/xmlversion.h: No such file or directory
    compilation terminated.
    Running C-compiler failed, exit 1
    VCL compilation failed


    Any ideas on whether my code is wrong, the parser.h file appears to be in the correct directory by default, so not sure where to start.

    Thankyou for any advice you can give,
    Dan
    Try it like this...
    Code:
    C{
      #include <syslog.h>
      #include <string.h>
      #include "libxml2/libxml/parser.h"
    }C
    The <> notation is for standard headers on the search path.
    Use "" notation for those you are specifying explicitly.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    The problem isn't finding libxml2/libxml/parser.h. Read the error message closely. That file was included correctly. But on line 15 of libxml2/libxml/parser.h, it tries to include libxml/xmlversion.h, which it can't find.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    The problem isn't finding libxml2/libxml/parser.h. Read the error message closely. That file was included correctly. But on line 15 of libxml2/libxml/parser.h, it tries to include libxml/xmlversion.h, which it can't find.
    Ahhh... I see that now. Nice catch!

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    So, on the note that the issue is with xmlversion.h, I ammended my file so it read:

    // Intialization
    C{
    #include <syslog.h>
    #include <string.h>
    #include <libxml2/libxml/xmlversion.h>
    #include <libxml2/libxml/parser.h>
    }C



    This causes the error to change to:

    retrobadger@retrobadger-desktop:~$ sudo /etc/init.d/varnish restart
    * Stopping HTTP accelerator varnishd [fail]
    * Starting HTTP accelerator varnishd [fail]
    storage_file: filename: /var/lib/varnish/retrobadger-desktop/varnish_storage.bin size 1024 MB.
    Message from C-compiler:
    In file included from ./vcl.1P9zoqAU.c:418:
    /usr/include/libxml2/libxml/xmlversion.h:13: fatal error: libxml/xmlexports.h: No such file or directory
    compilation terminated.
    Running C-compiler failed, exit 1
    VCL compilation failed


    So I then ammended to:
    // Intialization
    C{
    #include <syslog.h>
    #include <string.h>
    #include <libxml2/libxml/xmlexports.h>
    #include <libxml2/libxml/xmlversion.h>
    #include <libxml2/libxml/parser.h>
    }C


    So I think I may need to make a seperate file which includes the headers, then compile with gcc, and include that in here. Does this sound right, and if so, which function shall I use, just the normal include as above (using path to wherever I store my file)?
    But then the error persists, and will not go.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    It's the libxml2 includes that need to know where these files are. The easiest way is by fixing the command line options in the compiler. Back in post #3 I told you to add a -L... to the make file or script. That was a typo, -L is for library search path. You want -I for include search path, so something like -I/usr/include/libxml2.

    Where is the file you are using to compile all this? Is there a make file or a bash script or something I can look at?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Question

    @anduril462 Sorry for the confusion, there is no make file as such. What Varnish has is a VCL file (VCL) this file allows for some basic settings within varnish. What we would like to do is a bit more advanced than it allows (webservice request to tera-wurfl). So, we wanted to try and use the inline-c capabilities it holds, but we have little to no experience, so I wanted to try some basic scripts to teach myself what varnish/c can do together.

    varnish is started by running the command:
    sudo /etc/init.d/varnishd restart

    This is why I was thinking I would have to make a file for the headers which could be built using your recommendation, then call that singular file into the vcl file using include.

    Thankyou for your help and patience,
    Dan

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    I took a brief look at the Varnish website and didn't find much about the embedded/inline C features. We've answered the C half of this question -- specifying include file path. The rest of this is more of a Varnish question, so you might have better luck on one of the Varnish mailing lists (probably -misc): MailingLists.

    If you don't have any luck there, you can also download the source, scour for where it handles inline C and see if you can hack in the include paths you need. Better yet, fix it nicely, and submit a patch to the devs. Get yourself some open source cred.

  11. #11
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    With modern libraries, there is (or should be) no need to hard-code an install path (what if libxml2 is in /opt, or /usr/local/libxml2, or wherever?). Instead, libxml2 makes use of pkg-config, so you should use:
    Code:
    pkg-config libxml-2.0 --cflags
    when building, and
    Code:
    pkg-config libxml-2.0 --libs
    when linking. This takes care of include and library paths, and library names.

    If your system doesn't have pkg-config, libxml2 should also come with xml2-config, which performs the same function.

    pkg-config allows multiple packages to be specified, which is very handy:
    Code:
    pkg-config libfoo libbar libbaz --cflags
    Duplicate entries are merged into one (so if all set -I/usr/local/include, this is only output once).
    In GNU make, for example, you could do:
    Code:
    PACKAGES=	libxml-2.0 imlib2 x11
    CFLAGS+=	$(shell pkg-config $(PACKAGES) --cflags)
    LDADD+=		$(shell pkg-config $(PACKAGES) --libs)
    Encourage library authors to use pkg-config! Encourage library users to use pkg-config!

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Smile

    Thankyou for the help, thought I would post the 'potential' solution (although I am still getting some errors back).

    I have created a *.c and *.h file, then compiled these. Then the varnish vcl file includes the *.h file, from what I am understanding, this will then use the *.c file, and all the files included in it. My current issue is with running a function from the vcl file, which is housed in the *.c file, but I feel that is for a differen post, else this will get off topic.

    Thanks again for your guidance. If anyone has time, is there a good link which explains simply what the different file extensions are, and what compiling actually does. To compile my scripts I have been given 2 lines to use:

    gcc -c -o wurfl.o wurfl.c -I/usr/include/libxml2
    gcc -shared -Wl,-soname,libwurfl.so.1 -o libwurfl.so.1.0.1 wurfl.o -lxml2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-30-2010, 05:25 AM
  2. question about inline functions
    By ychen42 in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2010, 02:27 PM
  3. Easy beginner's question about pointers
    By kyldn6 in forum C Programming
    Replies: 4
    Last Post: 10-26-2010, 05:50 PM
  4. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  5. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 05:10 AM

Tags for this Thread


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