Thread: Header and Library help...

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    1

    Header and Library help...

    Hi all, first post, please be gentile.....

    I was doing an online tutorial on creating a simple library to generate .svg files. Sadly, the tutorial has disappeared from the 'net so I can't provide a direct link to it, but I don't think that is needed. I think this is a simple problem, but above my c knowledge.

    There are 3 files created doing the tutorial. The first is main.c

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<time.h>
    
    #include"svg.h"
    
    
    //--------------------------------------
    // FUNCTION PROTOTYPES
    //--------------------------------------
    void drawrectangles(void);
    void drawallshapes(void);
    void iwanttobelieve(void);
    void mondrian(void);
    
    
    //--------------------------------------
    // FUNCTION main
    //--------------------------------------
    int main()
    {
        puts("Code in C - SVG\n---------------\n");
    
        drawrectangles();
    
        //drawallshapes();
    
        //iwanttobelieve();
    
        //mondrian();
    
        return EXIT_SUCCESS;
    }
    
    
    // -------------------------------------
    // FUNCTION drawrectangles
    // -------------------------------------
    void drawrectangles(void)
    {
        svg* psvg;
        psvg = svg_create(512, 512);
    
        if(psvg == NULL)
        {
            puts("psvg is NULL");
        }
        else
        {
            svg_rectangle(psvg, 512, 512, 0, 0, "white", "white", 0, 0, 0);
    
            svg_rectangle(psvg, 384, 384, 64, 64, "#00FF00", "#000000", 4, 0, 0);
            svg_rectangle(psvg, 320, 320, 96, 96, "#FFFF00", "#000000", 2, 8, 8);
            svg_rectangle(psvg, 256, 256, 128, 128, "#00FFFF", "#000000", 2, 8, 8);
            svg_rectangle(psvg, 192, 192, 160, 160, "#FF80FF", "#000000", 2, 8, 8);
    
            svg_finalize(psvg);
            svg_save(psvg, "rectangles.svg");
            svg_free(psvg);
        }
    }
    the second file created is the header file, svg.h

    Code:
    #include<stdlib.h>
    
    #include<stdbool.h>
    
    #include<stdio.h>
    
    #include<math.h>
    
    
    //-------------------------------------
    // STRUCT svg
    //-------------------------------------
    typedef struct svg
    {
        char* svg;
    
        int height;
    
        int width;
    
        bool finalized;
    
    } svg;
    
    
    //-------------------------------------
    // FUNCTION PROTOTYPES
    //-------------------------------------
    svg* svg_create(int width, int height);
    
    void svg_finalize(svg* psvg);
    
    //void svg_print(svg* psvg);
    
    void svg_save(svg* psvg, char* filepath);
    
    void svg_free(svg* psvg);
    
    //void svg_circle(svg* psvg, char* stroke, int strokewidth, char* fill, int r, int cx, int cy);
    
    //void svg_line(svg* psvg, char* stroke, int strokewidth, int x1, int y1, int x2, int y2);
    
    void svg_rectangle(svg* psvg, int width, int height, int x, int y, char*  fill, char* stroke, int strokewidth, int radiusx, int radiusy);
    
    //void svg_fill(svg* psvg, char* fill);
    
    //void svg_text(svg* psvg, int x, int y, char* fontfamily, int fontsize, char* fill, char* stroke, char* text);
    
    //void svg_elipse(svg* psvg, int cx, int cy, int rx, int ry, char* fill, char* stroke, int strokewidth);
    The third file is the svg.c file, and it's kinda long, forgive me...

    Code:
    #include<stdlib.h>
    
    #include<stdbool.h>
    
    #include<stdio.h>
    
    #include<string.h>
    
    #include<math.h>
    
    #include"svg.h"
    
    //--------------------------------------
    // STATIC FUNCTION appendstringtosvg
    //--------------------------------------
    static void appendstringtosvg(svg* psvg, char* text)
    {
        int l = strlen(psvg->svg) + strlen(text) + 1;
    
        char* p = realloc(psvg->svg, l);
    
        if(p)
        {
            psvg->svg = p;
        }
    
        strcat(psvg->svg, text);
    }
    
    
    //--------------------------------------
    // STATIC FUNCTION appendnumbertosvg
    //--------------------------------------
    static void appendnumbertosvg(svg* psvg, int n)
    {
        char sn[16];
    
        sprintf(sn, "%d", n);
    
        appendstringtosvg(psvg, sn);
    }
    
    
    //--------------------------------------
    // FUNCTION svg_create
    //--------------------------------------
    svg* svg_create(int width, int height)
    {
        svg* psvg = malloc(sizeof(svg));
    
        if(psvg != NULL)
        {
            *psvg = (svg){.svg = NULL, .width = width, .height = height, .finalized = false};
    
            psvg->svg = malloc(1);
    
            sprintf(psvg->svg, "%s", "\0");
    
            appendstringtosvg(psvg, "<svg width='");
            appendnumbertosvg(psvg, width);
            appendstringtosvg(psvg, "px' height='");
            appendnumbertosvg(psvg, height);
            appendstringtosvg(psvg, "px' xmlns='http://www.w3.org/2000/svg'  version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink'>\n");
    
            return psvg;
        }
        else
        {
            return NULL;
        }
    }
    
    
    // -------------------------------------
    // FUNCTION svg_finalize
    // -------------------------------------
    void svg_finalize(svg* psvg)
    {
        appendstringtosvg(psvg, "</svg>");
    
        psvg->finalized = true;
    }
    
    
    //--------------------------------------
    // FUNCTION svg_rectangle
    //--------------------------------------
    void svg_rectangle(svg* psvg, int width, int height, int x, int y, char*  fill, char* stroke, int strokewidth, int radiusx, int radiusy)
    {
        appendstringtosvg(psvg, "    <rect fill='");
        appendstringtosvg(psvg, fill);
        appendstringtosvg(psvg, "' stroke='");
        appendstringtosvg(psvg, stroke);
        appendstringtosvg(psvg, "' stroke-width='");
        appendnumbertosvg(psvg, strokewidth);
        appendstringtosvg(psvg, "px' width='");
        appendnumbertosvg(psvg, width);
        appendstringtosvg(psvg, "' height='");
        appendnumbertosvg(psvg, height);
        appendstringtosvg(psvg, "' y='");
        appendnumbertosvg(psvg, y);
        appendstringtosvg(psvg, "' x='");
        appendnumbertosvg(psvg, x);
        appendstringtosvg(psvg, "' ry='");
        appendnumbertosvg(psvg, radiusy);
        appendstringtosvg(psvg, "' rx='");
        appendnumbertosvg(psvg, radiusx);
        appendstringtosvg(psvg, "' />\n");
    }
    
    
    // -------------------------------------
    // FUNCTION svg_save
    // -------------------------------------
    void svg_save(svg* psvg, char* filepath)
    {
        if(!psvg->finalized)
        {
            svg_finalize(psvg);
        }
    
        FILE* fp;
    
        fp = fopen(filepath, "w");
    
        if(fp != NULL)
        {
            fwrite(psvg->svg, 1, strlen(psvg->svg), fp);
    
            fclose(fp);
        }
    }
    
    
    //--------------------------------------
    // FUNCTION svg_free
    //--------------------------------------
    void svg_free(svg* psvg)
    {
        free(psvg->svg);
    
        free(psvg);
    }
    So heres the problem, when I compile this in codeblocks, I get an error in main.c
    Basically, the first use of a library function is unknown, like it's not been declared.

    If I change this line in main.c from this
    Code:
    #include"svg.h"
    to this
    Code:
    #include"svg.c"
    the code compiles and works fine. I'm using codeblocks as my IDE and I actually think that is where the problem is.

    here's a screenshot

    and the forum software won't allow me to attach an image...

    But basically, in codeblocks, on the left side of the screen where you can access the files of the code you're working on, the svg.h file is grayed out, like it's not available, but yet I can work on the code.

    I aplogize for the sloopiness of this post, but the lack of adding an image makes it harder to describe what I see...

    Thanks for any help,
    Randy



    Header and Library help...-screenshot-16-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,326
    Header files are always greyed out in code blocks.

    What happens when you try to build (press the yellow cog-wheel icon)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,715
    Compiles and runs just fine for me, without having to make the change you did. Are you sure you haven't messed something up? For example, did you initially create the *.c file as a *.cpp file and then renamed it? Because if you did, codeblocks isn't smart enough to change the compilation procedure, and ends up trying to compile it as a *.cpp file, and if your main compiled as a *.c file, the symbols don't match. You should right-click on both *.c files and go to Properties->Advanced, where the compiler variable should be CC, not CPP.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,629
    Make sure the svg.c is added to the project target that you are trying to build.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between header and library
    By c_lady in forum C Programming
    Replies: 9
    Last Post: 12-08-2010, 04:21 PM
  2. Need Help With Header library file..
    By kensam in forum C Programming
    Replies: 8
    Last Post: 01-27-2010, 01:39 PM
  3. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  4. Header and Library Files
    By jrahhali in forum C++ Programming
    Replies: 9
    Last Post: 08-23-2004, 03:51 PM
  5. Linux Library/Header's
    By biosx in forum Linux Programming
    Replies: 1
    Last Post: 08-12-2001, 09:14 PM

Tags for this Thread