linux使用pkg-config寫簡單的Makefile

來源:互聯網
上載者:User

將要編譯的代碼如下, 名字是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`

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.