glog 无法使用 MinGW 在 Windows 上编译

glog doesn't compile on Windows with MinGW

glog 似乎无法在 Windows 上使用 MinGW 进行编译。 Cmake 配置和生成没问题,但是当我启动 mingw32-make.exe -j10 命令时,我收到很多与歧义声明相关的错误。

这是完整的 cmake 配置和生成命令输出:

D:\INSTALL\Development\lib\ceres-mingw\glog\build>"D:\Program Files\CMake\bin\cmake.exe" -G "MinGW Makefiles" -Dgflags_DIR="D:\INSTALL\Development\lib\ceres-mingw\gflags\build" ..
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: D:/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for gflags namespace
-- Looking for gflags namespace - gflags
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Could NOT find Unwind (missing: Unwind_INCLUDE_DIR Unwind_LIBRARY Unwind_PLATFORM_LIBRARY)
-- Looking for dlfcn.h
-- Looking for dlfcn.h - found
-- Looking for execinfo.h
-- Looking for execinfo.h - not found
-- Looking for glob.h
-- Looking for glob.h - found
-- Looking for inttypes.h
-- Looking for inttypes.h - found
-- Looking for memory.h
-- Looking for memory.h - found
-- Looking for pwd.h
-- Looking for pwd.h - not found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stdlib.h
-- Looking for stdlib.h - found
-- Looking for string.h
-- Looking for string.h - found
-- Looking for strings.h
-- Looking for strings.h - found
-- Looking for sys/stat.h
-- Looking for sys/stat.h - found
-- Looking for sys/syscall.h
-- Looking for sys/syscall.h - not found
-- Looking for sys/time.h
-- Looking for sys/time.h - found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for sys/utsname.h
-- Looking for sys/utsname.h - not found
-- Looking for sys/wait.h
-- Looking for sys/wait.h - not found
-- Looking for syscall.h
-- Looking for syscall.h - not found
-- Looking for syslog.h
-- Looking for syslog.h - not found
-- Looking for ucontext.h
-- Looking for ucontext.h - not found
-- Looking for unistd.h
-- Looking for unistd.h - found
-- Looking for C++ include ext/hash_map
-- Looking for C++ include ext/hash_map - found
-- Looking for C++ include ext/hash_set
-- Looking for C++ include ext/hash_set - found
-- Looking for C++ include ext/slist
-- Looking for C++ include ext/slist - found
-- Looking for C++ include tr1/unordered_map
-- Looking for C++ include tr1/unordered_map - found
-- Looking for C++ include tr1/unordered_set
-- Looking for C++ include tr1/unordered_set - found
-- Looking for C++ include unordered_map
-- Looking for C++ include unordered_map - found
-- Looking for C++ include unordered_set
-- Looking for C++ include unordered_set - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of unsigned __int16
-- Check size of unsigned __int16 - done
-- Check size of u_int16_t
-- Check size of u_int16_t - failed
-- Check size of uint16_t
-- Check size of uint16_t - done
-- Looking for dladdr
-- Looking for dladdr - not found
-- Looking for fcntl
-- Looking for fcntl - not found
-- Looking for pread
-- Looking for pread - not found
-- Looking for pwrite
-- Looking for pwrite - not found
-- Looking for sigaction
-- Looking for sigaction - not found
-- Looking for sigaltstack
-- Looking for sigaltstack - not found
-- Performing Test HAVE_NO_DEPRECATED
-- Performing Test HAVE_NO_DEPRECATED - Success
-- Performing Test HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS
-- Performing Test HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS - Failed
-- Looking for snprintf
-- Looking for snprintf - found
-- Looking for UnDecorateSymbolName in dbghelp
-- Looking for UnDecorateSymbolName in dbghelp - not found
-- Performing Test HAVE___ATTRIBUTE__
-- Performing Test HAVE___ATTRIBUTE__ - Success
-- Performing Test HAVE___ATTRIBUTE__VISIBILITY_DEFAULT
-- Performing Test HAVE___ATTRIBUTE__VISIBILITY_DEFAULT - Success
-- Performing Test HAVE___ATTRIBUTE__VISIBILITY_HIDDEN
-- Performing Test HAVE___ATTRIBUTE__VISIBILITY_HIDDEN - Success
-- Performing Test HAVE___BUILTIN_EXPECT
-- Performing Test HAVE___BUILTIN_EXPECT - Success
-- Performing Test HAVE___SYNC_VAL_COMPARE_AND_SWAP
-- Performing Test HAVE___SYNC_VAL_COMPARE_AND_SWAP - Success
-- Performing Test HAVE_RWLOCK
-- Performing Test HAVE_RWLOCK - Failed
-- Performing Test HAVE___DECLSPEC
-- Performing Test HAVE___DECLSPEC - Success
-- Performing Test STL_NO_NAMESPACE
-- Performing Test STL_NO_NAMESPACE - Failed
-- Performing Test STL_STD_NAMESPACE
-- Performing Test STL_STD_NAMESPACE - Success
-- Performing Test HAVE_USING_OPERATOR
-- Performing Test HAVE_USING_OPERATOR - Success
-- Performing Test HAVE_NAMESPACES
-- Performing Test HAVE_NAMESPACES - Success
-- Performing Test HAVE_GCC_TLS
-- Performing Test HAVE_GCC_TLS - Success
-- Performing Test HAVE_MSVC_TLS
-- Performing Test HAVE_MSVC_TLS - Success
-- Performing Test HAVE_CXX11_TLS
-- Performing Test HAVE_CXX11_TLS - Success
-- Performing Test HAVE_ALIGNED_STORAGE
-- Performing Test HAVE_ALIGNED_STORAGE - Success
-- Performing Test HAVE_SYMBOLIZE
-- Performing Test HAVE_SYMBOLIZE - Failed
-- Performing Test HAVE_LOCALTIME_R
-- Performing Test HAVE_LOCALTIME_R - Failed
-- Configuring done
-- Generating done
-- Build files have been written to: D:/INSTALL/Development/lib/ceres-mingw/glog/build

这是mingw32编译命令的错误输出:

D:\INSTALL\Development\lib\ceres-mingw\glog\src\demangle.cc:42:10: fatal error: dbghelp.h: No such file or directory
   42 | #include <dbghelp.h>
      |          ^~~~~~~~~~~
compilation terminated.
CMakeFiles\glog.dir\build.make:62: recipe for target 'CMakeFiles/glog.dir/src/demangle.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/demangle.cc.obj] Error 1
mingw32-make[2]: *** Waiting for unfinished jobs....
In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\logging.cc:63:
D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows/dirent.h:383:28: error: missing binary operator before token "("
  383 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
      |                            ^
D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows/dirent.h:405:28: error: missing binary operator before token "("
  405 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
      |                            ^
D:\INSTALL\Development\lib\ceres-mingw\glog\src\symbolize.cc:877:10: fatal error: dbghelp.h: No such file or directory
  877 | #include <dbghelp.h>
      |          ^~~~~~~~~~~
compilation terminated.
In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc:41:
D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.h:166:14: error: ambiguating new declaration of 'char* strerror_r(int, char*, size_t)'
  166 | inline char* strerror_r(int errnum, char* buf, size_t buflen) {
      |              ^~~~~~~~~~
In file included from d:\mingw\include\winnt.h:49,
                 from d:\mingw\include\windef.h:269,
                 from d:\mingw\include\windows.h:42,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.h:52,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc:41:
d:\mingw\include\string.h:223:12: note: old declaration 'int strerror_r(int, char*, size_t)'
  223 | extern int strerror_r (int, char *, size_t);
      |            ^~~~~~~~~~C akeFiles\glog.dir\build.make:104: recipe for target 'CMakeFiles/glog.dir/src/symbolize.cc.obj' failed

mingw32-make[2]: *** [CMakeFiles/glog.dir/src/sy D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc:bo In function 'litm* localtime_r(const time_t*, tm*)ze':
.cD:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc:53:3:c. oberror: j]' Elocaltime_srr' was not declared in this scope; did you mean 'orlocaltime_r 1'?
   53 |
localtime_s(result, timep);
      |   ^~~~~~~~~~~
      |   localtime_r
D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc: In function 'tm* gmtime_r(const time_t*, tm*)':
D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows\port.cc:59:5: error: 'gmtime_s' was not declared in this scope; did you mean 'gmtime_r'?
   59 |     gmtime_s(result, timep);
      |     ^~~~~~~~
      |     gmtime_r
In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:80,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\raw_logging.cc:34:
D:/INSTALL/Development/lib/ceres-mingw/glog/src/windows/port.h:166:14: error: ambiguating new declaration of 'char* strerror_r(int, char*, size_t)'
  166 | inline char* strerror_r(int errnum, char* buf, size_t buflen) {
      |              ^~~~~~~~~~
In file included from d:\mingw\include\winnt.h:49,
                 from d:\mingw\include\windef.h:269,
                 from d:\mingw\include\windows.h:42,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\base/mutex.h:129,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:75,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\raw_logging.cc:34:
d:\mingw\include\string.h:223:12: note: old declaration 'int strerror_r(int, char*, size_t)'
  223 | extern int  MakeFiles\glog.dir\build.make:160: recipe for target 'CMakeFiles/glog.dirstrerror_r/ rc/windows/port.cc.obj' failed
 (int, char *, size_t);
      |            mi^~~~~~~~~~ng
w32-make[2]: *** [CMakeFiles/glog.dir/src/windows/port.cc.obj] Error 1
In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:80,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\logging.cc:32:
D:/INSTALL/Development/lib/ceres-mingw/glog/src/windows/port.h:166:14: error: ambiguating new declaration of 'char* strerror_r(int, char*, size_t)'
  166 | inline char* strerror_r(int errnum, char* buf, size_t buflen) {
      |              ^~~~~~~~~~
In file included from d:\mingw\include\winnt.h:49,
                 from d:\mingw\include\windef.h:269,
                 from d:\mingw\include\windows.h:42,
                 from In file included from                                                                                                                                                                                                                   from ,
                 from                                                                                                                                                                                                                                         from :
                                                                                                                                                                                                                                                                                       ambiguating new declaration of '                                                           '
  166 | inline char*                                                                                                                                                                                                                           223 | extern int (int errnum, char* buf, size_t buflen) {
      |                                                                                                                                                                                                                                            |
^~~~~~~~~~
In file included from d:\mingw\include\winnt.h:49,
                 from d:\mingw\include\windef.h:269,
                 from d:\mingw\include\windows.h:42,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\base/mutex.h:129,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:75,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.cc:32:
d:\mingw\include\string.h:223:12: note: old declaration 'int strerror_r(int, char*, size_t)'
  223 | extern int strerror_r (int, char *, size_t);
      |            ^~~~~~~~~~
In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:80,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\signalhandler.cc:34:
D:/INSTALL/Development/lib/ceres-mingw/glog/src/windows/port.h:166:14: error: ambiguating new declaration of 'char* strerror_r(int, char*, size_t)'
  166 | inline char* strerror_r(int errnum, char* buf, size_t buflen) {
      |              ^~~~~~~~~~
In file included from d:\mingw\include\winnt.h:49,
                 from d:\mingw\include\windef.h:269,
                 from d:\mingw\include\windows.h:42,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\base/mutex.h:129,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:75,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\signalhandler.cc:34:
d:\mingw\include\string.h:223:12: note: old declaration 'int strerror_r(int, char*, size_t)'
  223 | extern int strerror_r (int, char *, size_t);
      |            ^~~~~~~~~~
CMakeFiles\glog.dir\build.make:90: recipe for target 'CMakeFiles/glog.dir/src/raw_logging.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/raw_logging.cc.obj] Error 1
CMakeFiles\glog.dir\build.make:118: recipe for target 'CMakeFiles/glog.dir/src/utilities.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/utilities.cc.obj] Error 1
In file included from In file included from D:\INSTALL\Development\lib\ceres-mingw\glog\src\logging.cc:63                                                              ,
                 from :
D:\INSTALL\Development\lib\ceres-mingw\glog\src\vlog_is_on.cc:35D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows/dirent.h::
 In function 'D:/INSTALL/Development/lib/ceres-mingw/glog/src/windows/port.h:166:14:_WDIR* _wopendir(const wchar_t*) ':
error: D:\INSTALL\Development\lib\ceres-mingw\glog\src\windows/dirent.h:413:5:ambiguating new declaration of ' char* strerror_r(int, char*, size_t)error: '
  166 | inline char* 'strerror_rwcsncpy_s(int errnum, char* buf, size_t buflen) {
      |              ' was not declared in this scope; did you mean '^~~~~~~~~~wcsncpy
'?
  413 |     In file included from                                                                                                                                                                                                                  |     ,
                 from                                                                                                                                                                                                                              |     ,
                 from                                                                                                                                                                                                                        ,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\base/mutex.h:129,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\utilities.h:75,
                 from D:\INSTALL\Development\lib\ceres-mingw\glog\src\vlog_is_on.cc:35:
d:\mingw\include\string.h:223:12: note: old declaration 'int strerror_r(int, char*, size_t)'
  223 | extern int strerror_r (int, char *, size_t);
      |            ^~~~~~~~~~
CMakeFiles\glog.dir\build.make:146: recipe for target 'CMakeFiles/glog.dir/src/signalhandler.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/signalhandler.cc.obj] Error 1
D:\INSTALL\Development\lib\ceres-mingw\glog\src\logging.cc: In function 'void google::TruncateLogFile(const char*, google::int64, google::int64)':
D:\INSTALL\Development\lib\ceres-mingw\glog\src\logging.cc:2266:11: error: 'truncate' was not declared in this scope; did you mean 'ftruncate'?
 2266 |       if (truncate(path, 0) == -1) {
      |           ^~~~~~~~
      |           ftruncate
CMakeFiles\glog.dir\build.make:132: recipe for target 'CMakeFiles/glog.dir/src/vlog_is_on.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/vlog_is_on.cc.obj] Error 1
CMakeFiles\glog.dir\build.make:76: recipe for target 'CMakeFiles/glog.dir/src/logging.cc.obj' failed
mingw32-make[2]: *** [CMakeFiles/glog.dir/src/logging.cc.obj] Error 1
CMakeFiles\Makefile2:926: recipe for target 'CMakeFiles/glog.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/glog.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
D:\INSTALL\Development\lib\ceres-mingw\glog\build>

我用的是cmake 3.15.4版本和官方的mingw32,如下图:

C:\Users\plamen>"D:\Program Files\CMake\bin\cmake.exe" --version
cmake version 3.15.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

C:\Users\plamen>mingw32-make,exe --version
GNU Make 3.82.90
Built for i686-pc-mingw32
Copyright (C) 1988-2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

有没有人可以帮我解决这个问题?

我使用以下步骤使用 MinGW 在 Windows 上编译了 glog:

在我的 ceres 文件夹中(在我的例子中是“D:\INSTALL\Development\lib\ceres-mingw”)我输入了:

> git clone https://github.com/google/glog.git

之后我将“glog/BUILD”文件重命名为“_BUILD”并创建了一个名为'build'的文件夹。之后我进入新创建的 'build' 文件夹并发出这个命令:

> "D:\Program Files\CMake\bin\cmake.exe" -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DCMAKE_CXX_STANDARD_REQUIRED=ON -Dgflags_DIR="D:\INSTALL\Development\lib\ceres-mingw\gflags\build" ..
> "D:\MinGW64\mingw64\bin\mingw32-make.exe" -j10

重要提示:我从 'src\glog' 文件夹中复制了 'log_severity.h'(在我的例子中是 'D:\INSTALL\Development\lib\ceres-mingw\glog\src\glog') 到 'build\glog' 文件夹(在我的例子中是 'D:\INSTALL\Development\lib\ceres-mingw\glog\build\glog')。

因此,我有:

  • 所有包含文件位于
    'D:\INSTALL\Development\lib\ceres-mingw\glog\build\glog'

  • libglog.a 静态库位于
    'D:\INSTALL\Development\lib\ceres-mingw\glog\build'

  • glog-config.cmake 文件位于我的构建文件夹中(在我的例子中
    'D:\INSTALL\Development\lib\ceres-mingw\glog\build')

可选的一个可以安装(我没有):

> "D:\MinGW64\mingw64\bin\mingw32-make.exe" install

希望这些信息对其他用户也有用!

P.S.: 我很可能不得不重新编译 glog(也许还有 gflags 和 ceres),将 '-fPIC' 添加到 CMAKE_CXX_FLAGS,否则我在将 libopencv_java4.so 链接到 libglog.a 时出错在 Windows 上使用 MinGW 为 Android 编译 OpenCV。或者我在为 Android 编译 OpenCV 时必须使用共享库。我很快就会检查这个。