前进的道路不是一帆风顺的,要随时迎接挑战,敢于战胜困难!

坚持一下,找人聊聊天,释放一些压力!

 

linux使用pkg-config写简单的Makefile

将要编译的代码如下, 名字是add_node.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "null.h"
void parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword)
{
        xmlNewTextChild (cur, NULL, "keyword", keyword);

        cur = cur->xmlChildrenNode;
        while (cur != NULL)
        {
          printf("cur->name: %s\n", cur->name);
          cur = cur->next;
        }

        return;
}

xmlDocPtr parseDoc(char *docname, char *keyword)
{
        xmlDocPtr doc;
        xmlNodePtr cur;
        printf("before xmlParseFile->docname: %s\n", docname);
        doc = xmlParseFile(docname);
        printf("after xmlParseFile->docname: %s\n", docname);
        if (doc == NULL )
        {
          fprintf(stderr,"Document not parsed successfully. \n");
          return (NULL);
        }

        cur = xmlDocGetRootElement(doc);
        if (cur == NULL)
        {
          fprintf(stderr,"empty document\n");
          xmlFreeDoc(doc);
          return (NULL);
        }
        if (xmlStrcmp(cur->name, (const xmlChar *) "story"))
        {
          fprintf(stderr,"document of the wrong type, root node != story\n");
          xmlFreeDoc(doc);
          return (NULL);
        }
        cur = cur->xmlChildrenNode;
        while (cur != NULL)
        {
          if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo")))
          {
                parseStory (doc, cur, keyword);
          }
          cur = cur->next;
        }
        return(doc);
}

int main(int argc, char **argv)
{
        char *docname;
        char *keyword;
        xmlDocPtr doc;
        if (argc <= 2)
        {
          printf("Usage: %s docname, keyword\n", argv[0]);
          return(0);
        }
        printf("argv[0]: %s\n", argv[0]);
        docname = argv[1];
        printf("argv[1]: %s\n", argv[1]);
        keyword = argv[2];
        printf("argv[2]: %s\n", argv[2]);
        doc = parseDoc (docname, keyword);
        if (doc != NULL)
        {
          xmlSaveFormatFile (docname, doc, 0);
          xmlFreeDoc(doc);
        }
        return (1);
}

为了引导读者在使用Makefile编译更多的文件,写一个null.h,如下:

#include <time.h>
/*aaaa*/

实际上它是一个不干什么的公务员,只是充个名。

下面是Makefile的内容:

LIBXML_INC=$(shell pkg-config --cflags libxml-2.0)
CFLAGS += $(LIBXML_INC)
LIBXML_LIBS=$(shell pkg-config --libs libxml-2.0)
LIBS += $(LIBXML_LIBS)
aaa:add_node.c null.h
        $(CC) -o aaa $(CFLAGS) $(LIBS) $<

 

大家只要稍微留意一下会发现网上有不少pkg-config的介绍,这里就用到了他,用它的目的的使你的程序具有通用性,不至于只能在你的一亩三分地跑起来。使用make之后他便输出aaa,你输入:./aaa 1.xml suibianxieshenme ,边能发现1.xml增加了一个节点。

1.xml的格式如下,我已经运行几次了,所以看起来怪怪的:

<?xml version="1.0"?>
<story>
<storyinfo>
<author>John Fleck</author>
<datewritten>June 2, 2002</datewritten>
<keyword>example keyword</keyword>
<keyword>menj</keyword><keyword>mengjun</keyword></storyinfo>
<body>
<headline>This is the headline</headline>
<para>This is the body text.</para>
</body>
<reference uri="mengj" address="mengj" institute="mengj"/></story>

另外如果你就想用(刚开始我也想)包含pkg-congfig的gcc命令编译add_node.c,那你就使用下面的命令:

gcc -g add_node.c -o add_node  `pkg-config "libxml-2.0" --cflags --libs`




 

posted on 2009-05-18 10:10  山径山精  阅读(1949)  评论(0编辑  收藏  举报

导航