现在的位置: 首页 > 综合 > 正文

mingw编译log4c

2012年09月06日 ⁄ 综合 ⁄ 共 5552字 ⁄ 字号 评论关闭

1, 在mingw中配置log4c

configure --disable-doc --enable-reread --without-expat --prefix=`pwd`/dest && make -j && make install

小插曲:杀毒软件等,360,qq等,可能会把configure时候产生的conftest.exe误报为病毒,导致configure失败。

2, 修改examples/application_2.c

记住,这个时候,引用的头文件log4c.h依然是package自带的,等会我会创建一个自定义的,这2个文件确实在名字上是冲突的,
现在要引用的package自带的,否则编译不过。

/*
* This is one of log4c example programs.
*
* Notice how no relationships between the category and a certain
* priority, appender, or formatter are coded here.  These are all left
* to the log4crc config file so they can be chaned without recompiling
* 
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#ifndef _WIN32
#include <sys/time.h>
#else
#include <time.h>
#include <windows.h>
#include <winsock.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "log4c.h" // 引用package自带的头文件

#define L4C_API __declspec(dllexport)

L4C_API 
int l4c_init(void){
	return log4c_init();
}

L4C_API 
void * l4c_category_get(const char* a_name){
	return log4c_category_get(a_name);
}

L4C_API 
void l4c_category_log(const void * a_category
						, int a_priority
						, const char* a_format, ...){
    if (log4c_category_is_priority_enabled((log4c_category_t*)a_category
											, a_priority)) 
	{
		va_list va;
		va_start(va, a_format);
		log4c_category_vlog(a_category, a_priority, a_format, va);
		va_end(va);
    }
}

L4C_API 
int l4c_fini(void){
	return log4c_fini();
}

3, 编译生成log4c.dll

gcc -o log4c.dll -fPIC -shared -static -O0 -g -w -I../dest/include  application_2.c ../dest/lib/liblog4c.a

4, 使用dependency walker查看自定义函数的ordinal

也可以用dumpbin查看自定义函数在dll中位置

dumpbin /exports log4c.dll

Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file log4c.dll

File Type: DLL

  Section contains the following exports for log4c.dll

    00000000 characteristics
    516E5C71 time date stamp Wed Apr 17 16:25:21 2013
        0.00 version
           1 ordinal base
           4 number of functions
           4 number of names

    ordinal hint RVA      name

          1    0 0000158B l4c_category_get
          2    1 0000159E l4c_category_log
          3    2 000015DE l4c_fini
          4    3 0000157E l4c_init

  Summary

        1000 .CRT
        2000 .bss
        1000 .data
        5000 .debug_abbrev
        1000 .debug_aranges
        1000 .debug_frame
       18000 .debug_info
        5000 .debug_line
       11000 .debug_loc
        1000 .debug_pubnames
        1000 .debug_pubtypes
        1000 .debug_ranges
        1000 .debug_str
        1000 .edata
        3000 .eh_frame
        1000 .idata
        A000 .rdata
        1000 .reloc
        C000 .text
        1000 .tls

5,制作def文件

LIBRARY	"log4c"
DESCRIPTION "simple wrapper of log4c"
EXPORTS
    l4c_category_get	        @1
    l4c_category_log            @2
    l4c_fini                    @3
    l4c_init                    @4

6,生成lib

lib /def:log4c.def /out:log4c.lib /machine:x86

在msvc的命令行里可以执行dumpbin和lib等vc++ sdk的工具

7,制作一个简单的头文件

注意:
区别于package自带的头文件,这个是我自定义的,其实最好的做法是把这篇文章提到的wrapper库,以l4c开头,以区别原始的log4c。
#ifndef _LOG4C_WIN32_H_
#define _LOG4C_WIN32_H_

#ifdef WIN32
#ifdef L4C_EXPORTS
#    define L4C_API       __declspec(dllexport)
#else
#    define L4C_API       __declspec(dllimport)
#endif
#else
#    define L4C_API       
#endif

enum log4c_priority_level_t{
    /** fatal */    LOG4C_PRIORITY_FATAL    = 000, 
    /** alert */    LOG4C_PRIORITY_ALERT    = 100, 
    /** crit */     LOG4C_PRIORITY_CRIT     = 200, 
    /** error */    LOG4C_PRIORITY_ERROR    = 300, 
    /** warn */     LOG4C_PRIORITY_WARN     = 400, 
    /** notice */   LOG4C_PRIORITY_NOTICE   = 500, 
    /** info */     LOG4C_PRIORITY_INFO     = 600, 
    /** debug */    LOG4C_PRIORITY_DEBUG    = 700,
    /** trace */    LOG4C_PRIORITY_TRACE    = 800,
    /** notset */   LOG4C_PRIORITY_NOTSET   = 900,
    /** unknown */  LOG4C_PRIORITY_UNKNOWN  = 1000
} ;

#ifdef __cplusplus
extern "C" {
#endif

	L4C_API int l4c_init(void);

	L4C_API void * l4c_category_get(const char* a_name);

	L4C_API void l4c_category_log(const void * a_category
					, int a_priority
					, const char* a_format,...);

	L4C_API int l4c_fini(void);

#ifdef __cplusplus
};
#endif

#endif

8, 测试代码

#include "log4c.h"
#pragma comment(lib, "log4c.lib")

int main(int argc, char** argv)
{
  int i = 0;
  void* mycat = (void*)0;
  l4c_init();
  mycat = l4c_category_get("six13log.log.app.application2");
  for (i = 0; i < 100; i++)
	l4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 2");
  l4c_fini();
  return 0;
}

9, 测试用的配置文件log4crc

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE log4c SYSTEM "">

<log4c version="1.2.3">
    <config>
        <bufsize>0</bufsize>
        <debug level="2"/>
        <nocleanup>0</nocleanup>
        <reread>1</reread>
    </config>

    <category name="root" priority="notice"/>
    <category name="six13log.log" priority="error" appender="stdout" />
    <rollingpolicy name="myrollingpolicy" type="sizewin" maxsize="1024" maxnum="10" />
    <appender name="myrollingfileappender" type="rollingfile" logdir="." prefix="myprefix" layout="dated" rollingpolicy="myrollingpolicy" />
    <appender name="stdout" type="stream" layout="basic"/>
    <appender name="stderr" type="stream" layout="dated"/>
    <appender name="syslog" type="syslog" layout="basic"/>
    <appender name="s13file" type="s13_file" layout="basic"/>
    <appender name="plain_stderr" type="s13_stderr" layout="none"/>
    <appender name="cat_stderr" type="s13_stderr" layout="catlayout"/>
    <appender name="xml_stderr" type="s13_stderr" layout="xmllayout"/>
    <appender name="user_stderr" type="s13_stderr" layout="userlayout"/>
    <layout name="basic" type="basic"/>
    <layout name="dated" type="dated"/>
    <layout name="catlayout" type="s13_cat"/>
    <layout name="xmllayout" type="s13_xml"/>
    <layout name="none" type="s13_none"/>
    <layout name="userlayout" type="s13_userloc"/>
    <category name="six13log.log.app.application2" priority="debug" appender="cat_stderr" />
    <category name="six13log.log.app.application3" priority="debug" appender="user_stderr" />
    <category name="six13log.log.app" priority="debug" appender="myrollingfileappender" />
</log4c>

10, 编译测试代码

cl test.c

11, 运行测试代码

H:\log4c-1.2.3\examples\dlltest>test.exe
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2
[stdout] DEBUG    six13log.log.app.application2 - Debugging app 2

12, 截图

抱歉!评论已关闭.