vs2017環境下編譯log4cpp-1.1.3

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

blob.png

四、嘗試單獨編譯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」文件人編譯屬性,以下圖:日誌

blob.png

六、修改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」來禁用這段代碼。

blob.png

十一、從新編譯,搞定。

十二、其它工程一樣用以上方法修正。

 

此文轉自:http://www.jiazi.cn/blog/?id=55

相關文章
相關標籤/搜索