Log4cpp是一个开源的C++类库,它提供了在C++程序中使用日志和跟踪调试的功能。最新版本为1.1.3。项目地址为:http://log4cpp.sourceforge.net/windows
该版本指供了msvc6,msvc7(2003),msvc10(2010)的编译解决方案,其它编译解决方案能够从中选择一个进行调整。函数
如下是我msvc2017环境下的编译过程。post
一、下载并解压获得log4cpp源码;ui
二、复制一份msvc10子目录并重命名为msvc2017;this
三、用vs2017打开msvc10.sln解决方案,会提示须要升级,点击OK接受升级;spa
四、尝试单独编译log4cpp动态库,出现以下错误信息:.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1>------ Build started: Project: log4cpp, Configuration: Debug Win32 ------
1>Performing Custom Build Tools
1>'"vsvars32.bat"' 不是内部或外部命令,也不是可运行的程序
1>或批处理文件。
1>D:\log4cpp\msvc2017\log4cpp\..\NTEventLogCategories.mc : error : unable to open output file - Debug" -r Debug"\NTEventLogCategories.h
1>MC: Unable to open D:\log4cpp\msvc2017\log4cpp\..\NTEventLogCategories.mc for input
1>Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
1>
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>
1>CUSTOMBUILD : fatal error RC1110: could not open Debug\NTEventLogCategories.rc
1>
1>Microsoft (R) Incremental Linker Version 14.11.25508.2
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>LINK : fatal error LNK1181: cannot open input file 'Debug\NTEventLogCategories.res'
1>Done building project "log4cpp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
|
这是对“NTEventLogCategories.mc”文件编译的报错信息,该文件是采用“自定义编译”方法,用mc.exe命名行编译。调试
五、打开“NTEventLogCategories.mc”文件人编译属性,以下图:日志
六、修改Command Line。为了好看,我作了一下换行,换行后代码以下:code
1
2
3
4
5
6
7
8
9
|
"$(VS100COMNTOOLS)vsvars32.bat"
if not exist $(OutDir) md $(OutDir)
mc.exe -h "$(OutDir)" -r "$(OutDir)" "$(ProjectDir)..\%(Filename).mc"
rc.exe -r -fo "$(OutDir)%(Filename).res" "$(OutDir)%(Filename).rc"
link.exe /MACHINE:IX86 -dll -noentry -out:"$(OutDir)NTEventLogAppender.dll" "$(OutDir)%(Filename).res"
|
七、删除第一行,并在第三行的"$(OutDir)"改成"$(OutDir)\"。(即在后面多加一个\号)。改完后的全文以下:
1
2
3
4
5
6
7
|
if not exist $(OutDir) md $(OutDir)
mc.exe -h "$(OutDir)\" -r "$(OutDir)\" "$(ProjectDir)..\%(Filename).mc"
rc.exe -r -fo "$(OutDir)%(Filename).res" "$(OutDir)%(Filename).rc"
link.exe /MACHINE:IX86 -dll -noentry -out:"$(OutDir)NTEventLogAppender.dll" "$(OutDir)%(Filename).res"
|
八、从新编译,上面的问题没了,但又出现了另外一个错误。错误信息以下:
1
2
3
4
|
1>d:\log4cpp\src\snprintf.c(524): error C2084: function 'int snprintf(char *const ,const ::size_t,const char *const ,...)' already has a body
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\stdio.h(1938): note: see previous definition of 'snprintf'
1>d:\log4cpp\src\snprintf.c(538): error C2084: function 'int vsnprintf(char *const ,const ::size_t,const char *const ,va_list)' already has a body
1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\stdio.h(1428): note: see previous definition of 'vsnprintf'
|
九、根据错误提示定位到snprintf.c文件第524行,部份文本以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/*
* If the system does have snprintf and the portable routine is not
* specifically required, this module produces no code for snprintf/vsnprintf.
*/
#if !defined(HAVE_SNPRINTF) || defined(PREFER_PORTABLE_SNPRINTF)
#if !defined(NEED_SNPRINTF_ONLY)
int
portable_snprintf(
char
*str,
size_t
str_m,
const
char
*fmt,
/*args*/
...) {
va_list
ap;
int
str_l;
va_start
(ap, fmt);
str_l = portable_vsnprintf(str, str_m, fmt, ap);
va_end
(ap);
return
str_l;
}
|
十、能够看出log4cpp对snprintf作了从新定义,但vs2017已自带snprintf函数,因此,咱们能够用开关“HAVE_SNPRINTF”来禁用这段代码。
十一、从新编译,搞定。
十二、其它工程一样用以上方法修正。
此文转自:http://www.jiazi.cn/blog/?id=55