Upgrade gflags to v2.2.2
am: 3c6da7f2a8

Change-Id: Icf6487d6b48b73350612c95326ec8e787f8a6238
diff --git a/.gitignore b/.gitignore
index 321f70e..706f7f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
 /build/
 /builds/
 /build-*/
+/_build/
 .DS_Store
 CMakeCache.txt
 DartConfiguration.tcl
diff --git a/BUILD b/BUILD
index aa61563..0e7ccdd 100644
--- a/BUILD
+++ b/BUILD
@@ -6,7 +6,13 @@
 
 exports_files(["src/gflags_completions.sh", "COPYING.txt"])
 
+config_setting(
+    name = "x64_windows",
+    values = {"cpu": "x64_windows"},
+)
+
 load(":bazel/gflags.bzl", "gflags_sources", "gflags_library")
+
 (hdrs, srcs) = gflags_sources(namespace=["gflags", "google"])
 gflags_library(hdrs=hdrs, srcs=srcs, threads=0)
 gflags_library(hdrs=hdrs, srcs=srcs, threads=1)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 922a038..657a1f4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,9 +14,9 @@
 ##
 ## When this project is a subproject (GFLAGS_IS_SUBPROJECT is TRUE), the default
 ## settings are such that only the static single-threaded library is built without
-## installation of the gflags files. The "gflags" target is in this case an ALIAS
+## installation of the gflags files. The "gflags::gflags" target is in this case an ALIAS
 ## library target for the "gflags_nothreads_static" library target. Targets which
-## depend on the gflags library should link to the "gflags" library target.
+## depend on the gflags library should link to the "gflags::gflags" library target.
 ##
 ## Example CMakeLists.txt of user project which requires separate gflags installation:
 ##   cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
@@ -26,7 +26,7 @@
 ##   find_package(gflags REQUIRED)
 ##
 ##   add_executable(foo src/foo.cc)
-##   target_link_libraries(foo gflags)
+##   target_link_libraries(foo gflags::gflags)
 ##
 ## Example CMakeLists.txt of user project which requires separate single-threaded static gflags installation:
 ##   cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
@@ -36,7 +36,7 @@
 ##   find_package(gflags COMPONENTS nothreads_static)
 ##
 ##   add_executable(foo src/foo.cc)
-##   target_link_libraries(foo gflags)
+##   target_link_libraries(foo gflags::gflags)
 ##
 ## Example CMakeLists.txt of super-project which contains gflags source tree:
 ##   cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
@@ -46,7 +46,7 @@
 ##   add_subdirectory(gflags)
 ##
 ##   add_executable(foo src/foo.cc)
-##   target_link_libraries(foo gflags)
+##   target_link_libraries(foo gflags::gflags)
 ##
 ## Variables to configure the source files:
 ## - GFLAGS_IS_A_DLL
@@ -70,12 +70,16 @@
 ## - GFLAGS_INSTALL_SHARED_LIBS
 ## - GFLAGS_INSTALL_STATIC_LIBS
 
-cmake_minimum_required (VERSION 2.8.12 FATAL_ERROR)
+cmake_minimum_required (VERSION 3.0.2 FATAL_ERROR)
 
 if (POLICY CMP0042)
   cmake_policy (SET CMP0042 NEW)
 endif ()
 
+if (POLICY CMP0048)
+  cmake_policy (SET CMP0048 NEW)
+endif ()
+
 # ----------------------------------------------------------------------------
 # includes
 include ("${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake")
@@ -83,14 +87,14 @@
 # ----------------------------------------------------------------------------
 # package information
 set (PACKAGE_NAME        "gflags")
-set (PACKAGE_VERSION     "2.2.1")
+set (PACKAGE_VERSION     "2.2.2")
 set (PACKAGE_STRING      "${PACKAGE_NAME} ${PACKAGE_VERSION}")
 set (PACKAGE_TARNAME     "${PACKAGE_NAME}-${PACKAGE_VERSION}")
 set (PACKAGE_BUGREPORT   "https://github.com/gflags/gflags/issues")
 set (PACKAGE_DESCRIPTION "A commandline flags library that allows for distributed flags.")
 set (PACKAGE_URL         "http://gflags.github.io/gflags")
 
-project (${PACKAGE_NAME} CXX)
+project (${PACKAGE_NAME} VERSION ${PACKAGE_VERSION} LANGUAGES CXX)
 if (CMAKE_VERSION VERSION_LESS 3.4)
   # C language still needed because the following required CMake modules
   # (or their dependencies, respectively) are not correctly handling
@@ -403,6 +407,9 @@
   set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
   set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
 endif ()
+# Set postfixes for generated libraries based on buildtype.
+set(CMAKE_RELEASE_POSTFIX "")
+set(CMAKE_DEBUG_POSTFIX "_debug")
 
 # ----------------------------------------------------------------------------
 # installation directories
@@ -491,11 +498,15 @@
   foreach (type IN ITEMS static shared)
     foreach (opts IN ITEMS "_nothreads" "")
       if (TARGET gflags${opts}_${type})
+        # Define "gflags" alias for super-projects treating targets of this library as part of their own project
+        # (also for backwards compatibility with gflags 2.2.1 which only defined this alias)
         add_library (gflags ALIAS gflags${opts}_${type})
+        # Define "gflags::gflags" alias for projects that support both find_package(gflags) and add_subdirectory(gflags)
+        add_library (gflags::gflags ALIAS gflags${opts}_${type})
         break ()
       endif ()
     endforeach ()
-    if (TARGET gflags)
+    if (TARGET gflags::gflags)
        break ()
     endif ()
   endforeach ()
@@ -515,7 +526,8 @@
                EXPORT ${EXPORT_NAME}
                RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
                LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
-               ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR})
+               ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
+      )
     endif ()
   endforeach ()
 endif ()
@@ -526,7 +538,8 @@
                EXPORT ${EXPORT_NAME}
                RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
                LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
-               ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR})
+               ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
+      )
     endif ()
   endforeach ()
 endif ()
@@ -542,7 +555,16 @@
     FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
     DESTINATION ${CONFIG_INSTALL_DIR}
   )
-  install (EXPORT ${EXPORT_NAME} DESTINATION ${CONFIG_INSTALL_DIR})
+  install (
+    EXPORT ${EXPORT_NAME}
+    NAMESPACE ${PACKAGE_NAME}::
+    DESTINATION ${CONFIG_INSTALL_DIR}
+  )
+  install (
+    EXPORT ${EXPORT_NAME}
+    FILE ${PACKAGE_NAME}-nonamespace-targets.cmake
+    DESTINATION ${CONFIG_INSTALL_DIR}
+  )
   if (UNIX)
     install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
   endif ()
@@ -556,7 +578,15 @@
 # ----------------------------------------------------------------------------
 # support direct use of build tree
 set (INSTALL_PREFIX_REL2CONFIG_DIR .)
-export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake")
+export (
+  TARGETS ${TARGETS}
+  NAMESPACE ${PACKAGE_NAME}::
+  FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake"
+)
+export (
+  TARGETS ${TARGETS}
+  FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-nonamespace-targets.cmake"
+)
 if (REGISTER_BUILD_DIR)
   export (PACKAGE ${PACKAGE_NAME})
 endif ()
@@ -569,7 +599,6 @@
 # testing - MUST follow the generation of the build tree config file
 if (BUILD_TESTING)
   include (CTest)
-  enable_testing ()
   add_subdirectory (test)
 endif ()
 
@@ -589,7 +618,8 @@
                      "\n  BUILD_STATIC_LIBS=ON"
                      "\n  INSTALL_HEADERS=ON"
                      "\n  INSTALL_SHARED_LIBS=ON"
-                     "\n  INSTALL_STATIC_LIBS=ON")
+                     "\n  INSTALL_STATIC_LIBS=ON"
+    )
   endif ()
 
   # default package generators
diff --git a/ChangeLog.txt b/ChangeLog.txt
index c26d0ab..ecdd465 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -1,3 +1,25 @@
+* Sun Nov 11 2018 - Andreas Schuh <andreas.schuh.84@gmail.com>
+
+- gflags: version 2.2.2
+Fixed 267: Support build with GCC option "-fvisibility=hidden".
+Fixed 262: Declare FALGS_no##name variables as static to avoid "previous extern" warning.
+Fixed 261: Declare FlagRegisterer c’tor explicit template instanations as extern in header
+Fixed 257: Build with _UNICODE support on Windows.
+Fixed 233/234/235: Move CMake defines that are unused by Bazel to separate header; makes config.h private again
+Fixed 228: Build with recent MinGW versions that define setenv.
+Fixed 226: Remove obsolete and unused CleanFileName code
+Merged 266: Various PVS Studio and GCC warnings.
+Merged 258: Fix build with some Clang variants that define "restrict" macro.
+Merged 252: Update documentation on how to use Bazel.
+Merged 249: Use "_debug" postfix for debug libraries. 
+Merged 247: CMake "project" VERSION; no enable_testing(); "gflags::" import target prefix.
+Merged 246: Add Bazel-on-Windows support.
+Merged 239: Use GFLAGS_NAMESPACE instead of "gflags" in test executable.
+Merged 237: Removed unused functions; fixes compilation with -Werror compiler option.
+Merged 232: Fix typo in Bazel's BUILD definition
+Merged 230: Remove using ::fLS::clstring.
+Merged 221: Add convenience 'uninstall' target
+
 * Tue Jul 11 2017 - Andreas Schuh <andreas.schuh.84@gmail.com>
 
 - gflags: version 2.2.1
diff --git a/INSTALL.md b/INSTALL.md
index d37528f..76d7edd 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -78,11 +78,6 @@
         remote = "https://github.com/gflags/gflags.git",
     )
 
-    bind(
-        name = "gflags",
-        actual = "@com_github_gflags_gflags//:gflags",
-    )
-
-You can then add `//external:gflags` to the `deps` section of a `cc_binary` or
-`cc_library` rule, and `#include <gflags/gflags.h>` to include it in your source
-code.
+You can then add `@com_github_gflags_gflags//:gflags` to the `deps` section of a
+`cc_binary` or `cc_library` rule, and `#include <gflags/gflags.h>` to include it
+in your source code.
diff --git a/METADATA b/METADATA
index 81f1b2a..37eaaf0 100644
--- a/METADATA
+++ b/METADATA
@@ -1,11 +1,5 @@
 name: "gflags"
-description:
-    "The gflags package contains a C++ library that implements commandline "
-    "flags processing. It includes built-in support for standard types such as "
-    "string and the ability to define flags in the source file in which they "
-    "are used. Online documentation available at: "
-    "https://gflags.github.io/gflags/"
-
+description: "The gflags package contains a C++ library that implements commandline flags processing. It includes built-in support for standard types such as string and the ability to define flags in the source file in which they are used. Online documentation available at: https://gflags.github.io/gflags/"
 third_party {
   url {
     type: HOMEPAGE
@@ -15,6 +9,10 @@
     type: GIT
     value: "https://github.com/gflags/gflags.git"
   }
-  version: "2.2.1"
-  last_upgrade_date { year: 2017 month: 10 day: 10 }
-}
\ No newline at end of file
+  version: "v2.2.2"
+  last_upgrade_date {
+    year: 2019
+    month: 2
+    day: 1
+  }
+}
diff --git a/README.md b/README.md
index 9f3e3f2..6e5267c 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,21 @@
 
 The documentation of the gflags library is available online at https://gflags.github.io/gflags/.
 
+
+11 November 2018
+----------------
+
+I've just released gflags 2.2.2.
+
+This maintenance release improves lives of Bazel users (no more "config.h" leaking into global include paths),
+fixes build with recent MinGW versions, and silences a number of static code analyzer and compiler warnings.
+The build targets exported by the CMake configuration of this library are now also prefixed by the package
+name "gflags::" following a more recent (unwritten) CMake convention. The unprefixed target names are still
+supported to avoid that dependent projects have to be modified due to this change in imported target names.
+
+Please report any further issues with this release using the GitHub issue tracker.
+
+
 11 July 2017
 ------------
 
diff --git a/bazel/gflags.bzl b/bazel/gflags.bzl
index 7bf91cd..533fd61 100644
--- a/bazel/gflags.bzl
+++ b/bazel/gflags.bzl
@@ -47,8 +47,14 @@
         "src/gflags_completions.cc",
         "src/gflags_reporting.cc",
         "src/mutex.h",
-        "src/util.h"
-    ]
+        "src/util.h",
+    ] + select({
+        "//:x64_windows": [
+            "src/windows_port.cc",
+            "src/windows_port.h",
+        ],
+        "//conditions:default": [],
+    })
     return [hdrs, srcs]
 
 # ------------------------------------------------------------------------------
@@ -64,19 +70,28 @@
         "-DHAVE_SYS_TYPES_H",
         "-DHAVE_INTTYPES_H",
         "-DHAVE_SYS_STAT_H",
-        "-DHAVE_UNISTD_H",
-        "-DHAVE_FNMATCH_H",
         "-DHAVE_STRTOLL",
         "-DHAVE_STRTOQ",
-        "-DHAVE_PTHREAD",
         "-DHAVE_RWLOCK",
-    ]
+    ] + select({
+        "//:x64_windows": [
+            "-DOS_WINDOWS",
+        ],
+        "//conditions:default": [
+            "-DHAVE_UNISTD_H",
+            "-DHAVE_FNMATCH_H",
+            "-DHAVE_PTHREAD",
+        ],
+    })
     linkopts = []
     if threads:
-        linkopts.append("-lpthread")
+        linkopts += select({
+            "//:x64_windows": [],
+            "//conditions:default": ["-lpthread"],
+        })
     else:
         name += "_nothreads"
-        copts.append("-DNO_THREADS")
+        copts += ["-DNO_THREADS"]
     native.cc_library(
         name       = name,
         hdrs       = hdrs,
diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in
index a121b3a..a512c2a 100644
--- a/cmake/config.cmake.in
+++ b/cmake/config.cmake.in
@@ -7,7 +7,21 @@
 set (@PACKAGE_PREFIX@_VERSION_PATCH  @PACKAGE_VERSION_PATCH@)
 
 # import targets
-include ("${CMAKE_CURRENT_LIST_DIR}/@EXPORT_NAME@.cmake")
+if (NOT DEFINED @PACKAGE_PREFIX@_USE_TARGET_NAMESPACE)
+  set (@PACKAGE_PREFIX@_USE_TARGET_NAMESPACE FALSE)
+endif ()
+if (@PACKAGE_PREFIX@_USE_TARGET_NAMESPACE)
+  include ("${CMAKE_CURRENT_LIST_DIR}/@EXPORT_NAME@.cmake")
+  set (@PACKAGE_PREFIX@_TARGET_NAMESPACE @PACKAGE_NAME@)
+else ()
+  include ("${CMAKE_CURRENT_LIST_DIR}/@PACKAGE_NAME@-nonamespace-targets.cmake")
+  set (@PACKAGE_PREFIX@_TARGET_NAMESPACE)
+endif ()
+if (@PACKAGE_PREFIX@_TARGET_NAMESPACE)
+  set (@PACKAGE_PREFIX@_TARGET_PREFIX ${@PACKAGE_PREFIX@_TARGET_NAMESPACE}::)
+else ()
+  set (@PACKAGE_PREFIX@_TARGET_PREFIX)
+endif ()
 
 # installation prefix
 get_filename_component (CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
@@ -49,7 +63,7 @@
     else ()
       set (@PACKAGE_PREFIX@_SHARED FALSE)
     endif ()
-  elseif (TARGET @PACKAGE_NAME@_shared OR TARGET @PACKAGE_NAME@_nothreads_shared)
+  elseif (TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_shared OR TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_nothreads_shared)
     set (@PACKAGE_PREFIX@_SHARED TRUE)
   else ()
     set (@PACKAGE_PREFIX@_SHARED FALSE)
@@ -64,7 +78,7 @@
     else ()
       set (@PACKAGE_PREFIX@_NOTHREADS FALSE)
     endif ()
-  elseif (TARGET @PACKAGE_NAME@_static OR TARGET @PACKAGE_NAME@_shared)
+  elseif (TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}PACKAGE_NAME@_static OR TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_shared)
     set (@PACKAGE_PREFIX@_NOTHREADS FALSE)
   else ()
     set (@PACKAGE_PREFIX@_NOTHREADS TRUE)
@@ -77,15 +91,15 @@
     set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_NAME@_TARGET})
   elseif (@PACKAGE_PREFIX@_SHARED)
     if (@PACKAGE_PREFIX@_NOTHREADS)
-      set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads_shared)
+      set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_nothreads_shared)
     else ()
-      set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_shared)
+      set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_shared)
     endif ()
   else ()
     if (@PACKAGE_PREFIX@_NOTHREADS)
-      set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_nothreads_static)
+      set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_nothreads_static)
     else ()
-      set (@PACKAGE_PREFIX@_TARGET @PACKAGE_NAME@_static)
+      set (@PACKAGE_PREFIX@_TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@_static)
     endif ()
   endif ()
 endif ()
@@ -94,12 +108,12 @@
                        " Try a different combination of @PACKAGE_PREFIX@_SHARED and @PACKAGE_PREFIX@_NOTHREADS.")
 endif ()
 
-# add more convenient "@PACKAGE_NAME@" import target
-if (NOT TARGET @PACKAGE_NAME@)
+# add more convenient "${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@" import target
+if (NOT TARGET ${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@)
   if (@PACKAGE_PREFIX@_SHARED)
-    add_library (@PACKAGE_NAME@ SHARED IMPORTED)
+    add_library (${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@ SHARED IMPORTED)
   else ()
-    add_library (@PACKAGE_NAME@ STATIC IMPORTED)
+    add_library (${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@ STATIC IMPORTED)
   endif ()
   # copy INTERFACE_* properties
   foreach (_@PACKAGE_PREFIX@_PROPERTY_NAME IN ITEMS
@@ -112,20 +126,20 @@
   )
     get_target_property (_@PACKAGE_PREFIX@_PROPERTY_VALUE ${@PACKAGE_PREFIX@_TARGET} INTERFACE_${_@PACKAGE_PREFIX@_PROPERTY_NAME})
     if (_@PACKAGE_PREFIX@_PROPERTY_VALUE)
-      set_target_properties(@PACKAGE_NAME@ PROPERTIES
+      set_target_properties(${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@ PROPERTIES
         INTERFACE_${_@PACKAGE_PREFIX@_PROPERTY_NAME} "${_@PACKAGE_PREFIX@_PROPERTY_VALUE}"
       )
     endif ()
   endforeach ()
   # copy IMPORTED_*_<CONFIG> properties
   get_target_property (_@PACKAGE_PREFIX@_CONFIGURATIONS ${@PACKAGE_PREFIX@_TARGET} IMPORTED_CONFIGURATIONS)
-  set_target_properties (@PACKAGE_NAME@ PROPERTIES IMPORTED_CONFIGURATIONS "${_@PACKAGE_PREFIX@_CONFIGURATIONS}")
+  set_target_properties (${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@ PROPERTIES IMPORTED_CONFIGURATIONS "${_@PACKAGE_PREFIX@_CONFIGURATIONS}")
   foreach (_@PACKAGE_PREFIX@_PROPERTY_NAME IN ITEMS
     IMPLIB
     LOCATION
     LINK_DEPENDENT_LIBRARIES
     LINK_INTERFACE_LIBRARIES
-    LINK_INTERFACE_LANGUAGES 
+    LINK_INTERFACE_LANGUAGES
     LINK_INTERFACE_MULTIPLICITY
     NO_SONAME
     SONAME
@@ -133,7 +147,7 @@
     foreach (_@PACKAGE_PREFIX@_CONFIG IN LISTS _@PACKAGE_PREFIX@_CONFIGURATIONS)
       get_target_property (_@PACKAGE_PREFIX@_PROPERTY_VALUE ${@PACKAGE_PREFIX@_TARGET} IMPORTED_${_@PACKAGE_PREFIX@_PROPERTY_NAME}_${_@PACKAGE_PREFIX@_CONFIG})
       if (_@PACKAGE_PREFIX@_PROPERTY_VALUE)
-        set_target_properties(@PACKAGE_NAME@ PROPERTIES
+        set_target_properties(${@PACKAGE_PREFIX@_TARGET_PREFIX}@PACKAGE_NAME@ PROPERTIES
           IMPORTED_${_@PACKAGE_PREFIX@_PROPERTY_NAME}_${_@PACKAGE_PREFIX@_CONFIG} "${_@PACKAGE_PREFIX@_PROPERTY_VALUE}"
         )
       endif ()
diff --git a/src/config.h b/src/config.h
index b90b7e6..c33d207 100644
--- a/src/config.h
+++ b/src/config.h
@@ -36,6 +36,8 @@
 #ifndef GFLAGS_DLL_DECL
 #  if GFLAGS_IS_A_DLL && defined(_MSC_VER)
 #    define GFLAGS_DLL_DECL __declspec(dllexport)
+#  elif defined(__GNUC__) && __GNUC__ >= 4
+#    define GFLAGS_DLL_DECL __attribute__((visibility("default")))
 #  else
 #    define GFLAGS_DLL_DECL
 #  endif
diff --git a/src/gflags.cc b/src/gflags.cc
index 9869782..8f5aa0b 100644
--- a/src/gflags.cc
+++ b/src/gflags.cc
@@ -1036,17 +1036,15 @@
     char* arg = (*argv)[i];
 
     // Like getopt(), we permute non-option flags to be at the end.
-    if (arg[0] != '-' ||           // must be a program argument
-        (arg[0] == '-' && arg[1] == '\0')) {  // "-" is an argument, not a flag
+    if (arg[0] != '-' || arg[1] == '\0') {	// must be a program argument: "-" is an argument, not a flag
       memmove((*argv) + i, (*argv) + i+1, (*argc - (i+1)) * sizeof((*argv)[i]));
       (*argv)[*argc-1] = arg;      // we go last
       first_nonopt--;              // we've been pushed onto the stack
       i--;                         // to undo the i++ in the loop
       continue;
     }
-
-    if (arg[0] == '-') arg++;      // allow leading '-'
-    if (arg[0] == '-') arg++;      // or leading '--'
+    arg++;                     // skip leading '-'
+    if (arg[0] == '-') arg++;  // or leading '--'
 
     // -- alone means what it does for GNU: stop options parsing
     if (*arg == '\0') {
@@ -1343,8 +1341,8 @@
             || fnmatch(glob.c_str(), ProgramInvocationName(),      FNM_PATHNAME) == 0
             || fnmatch(glob.c_str(), ProgramInvocationShortName(), FNM_PATHNAME) == 0
 #elif defined(HAVE_SHLWAPI_H)
-            || PathMatchSpec(glob.c_str(), ProgramInvocationName())
-            || PathMatchSpec(glob.c_str(), ProgramInvocationShortName())
+            || PathMatchSpecA(glob.c_str(), ProgramInvocationName())
+            || PathMatchSpecA(glob.c_str(), ProgramInvocationShortName())
 #endif
             ) {
           flags_are_relevant = true;
diff --git a/src/gflags.h.in b/src/gflags.h.in
index 82e640f..7b218b9 100644
--- a/src/gflags.h.in
+++ b/src/gflags.h.in
@@ -441,6 +441,27 @@
                  FlagType* current_storage, FlagType* defvalue_storage);
 };
 
+// Force compiler to not generate code for the given template specialization.
+#if defined(_MSC_VER) && _MSC_VER < 1800 // Visual Studio 2013 version 12.0
+  #define GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(type)
+#else
+  #define GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(type)                  \
+    extern template GFLAGS_DLL_DECL FlagRegisterer::FlagRegisterer(  \
+        const char* name, const char* help, const char* filename,    \
+        type* current_storage, type* defvalue_storage)
+#endif
+
+// Do this for all supported flag types.
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(bool);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(int32);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(uint32);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(int64);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(uint64);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(double);
+GFLAGS_DECLARE_FLAG_REGISTERER_CTOR(std::string);
+
+#undef GFLAGS_DECLARE_FLAG_REGISTERER_CTOR
+
 // If your application #defines STRIP_FLAG_HELP to a non-zero value
 // before #including this file, we remove the help message from the
 // binary file. This can reduce the size of the resulting binary
@@ -478,7 +499,7 @@
     static const type FLAGS_nono##name = value;                         \
     /* We always want to export defined variables, dll or no */         \
     GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = FLAGS_nono##name;        \
-    type FLAGS_no##name = FLAGS_nono##name;                             \
+    static type FLAGS_no##name = FLAGS_nono##name;                      \
     static GFLAGS_NAMESPACE::FlagRegisterer o_##name(                   \
       #name, MAYBE_STRIPPED_HELP(help), __FILE__,                       \
       &FLAGS_##name, &FLAGS_no##name);                                  \
diff --git a/src/gflags_completions.cc b/src/gflags_completions.cc
index f772486..c53a128 100644
--- a/src/gflags_completions.cc
+++ b/src/gflags_completions.cc
@@ -179,6 +179,11 @@
   bool flag_description_substring_search;
   bool return_all_matching_flags;
   bool force_no_update;
+  CompletionOptions(): flag_name_substring_search(false),
+                       flag_location_substring_search(false),
+                       flag_description_substring_search(false),
+                       return_all_matching_flags(false),
+                       force_no_update(false) { }
 };
 
 // Notable flags are flags that are special or preferred for some
@@ -202,7 +207,7 @@
 static void PrintFlagCompletionInfo(void) {
   string cursor_word = FLAGS_tab_completion_word;
   string canonical_token;
-  CompletionOptions options = { };
+  CompletionOptions options = CompletionOptions();
   CanonicalizeCursorWordAndSearchOptions(
       cursor_word,
       &canonical_token,
@@ -545,8 +550,7 @@
 
   vector<DisplayInfoGroup> output_groups;
   bool perfect_match_found = false;
-  if (lines_so_far < max_desired_lines &&
-      !notable_flags->perfect_match_flag.empty()) {
+  if (!notable_flags->perfect_match_flag.empty()) {
     perfect_match_found = true;
     DisplayInfoGroup group =
         { "",
diff --git a/src/gflags_declare.h.in b/src/gflags_declare.h.in
index 752a34d..ab7bd24 100644
--- a/src/gflags_declare.h.in
+++ b/src/gflags_declare.h.in
@@ -58,6 +58,8 @@
 #ifndef GFLAGS_DLL_DECL
 #  if GFLAGS_IS_A_DLL && defined(_MSC_VER)
 #    define GFLAGS_DLL_DECL __declspec(dllimport)
+#  elif defined(__GNUC__) && __GNUC__ >= 4
+#    define GFLAGS_DLL_DECL __attribute__((visibility("default")))
 #  else
 #    define GFLAGS_DLL_DECL
 #  endif
@@ -67,6 +69,8 @@
 #ifndef GFLAGS_DLL_DECLARE_FLAG
 #  if GFLAGS_IS_A_DLL && defined(_MSC_VER)
 #    define GFLAGS_DLL_DECLARE_FLAG __declspec(dllimport)
+#  elif defined(__GNUC__) && __GNUC__ >= 4
+#    define GFLAGS_DLL_DECLARE_FLAG __attribute__((visibility("default")))
 #  else
 #    define GFLAGS_DLL_DECLARE_FLAG
 #  endif
diff --git a/src/gflags_reporting.cc b/src/gflags_reporting.cc
index 7cc6691..29be922 100644
--- a/src/gflags_reporting.cc
+++ b/src/gflags_reporting.cc
@@ -296,10 +296,10 @@
   }
 }
 
-void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict) {
+void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict_) {
   vector<string> substrings;
-  if (restrict != NULL && *restrict != '\0') {
-    substrings.push_back(restrict);
+  if (restrict_ != NULL && *restrict_ != '\0') {
+    substrings.push_back(restrict_);
   }
   ShowUsageWithFlagsMatching(argv0, substrings);
 }
@@ -389,8 +389,8 @@
     gflags_exitfunc(1);
 
   } else if (!FLAGS_helpon.empty()) {
-    string restrict = PATH_SEPARATOR + FLAGS_helpon + ".";
-    ShowUsageWithFlagsRestrict(progname, restrict.c_str());
+    string restrict_ = PATH_SEPARATOR + FLAGS_helpon + ".";
+    ShowUsageWithFlagsRestrict(progname, restrict_.c_str());
     gflags_exitfunc(1);
 
   } else if (!FLAGS_helpmatch.empty()) {
diff --git a/src/windows_port.h b/src/windows_port.h
index 61cf5b7..59a310e 100644
--- a/src/windows_port.h
+++ b/src/windows_port.h
@@ -77,6 +77,7 @@
 #  pragma warning(push)
 #  pragma warning(disable: 4996) // ignore getenv security warning
 #endif
+#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
 inline void setenv(const char* name, const char* value, int) {
   // In windows, it's impossible to set a variable to the empty string.
   // We handle this by setting it to "0" and the NUL-ing out the \0.
@@ -98,6 +99,7 @@
       *getenv(name) = '\0';            // works when putenv() copies nameval
   }
 }
+#endif
 #ifdef _MSC_VER
 #  pragma warning(pop)
 #endif
diff --git a/test/config/CMakeLists.txt b/test/config/CMakeLists.txt
index c54f54b..6190b25 100644
--- a/test/config/CMakeLists.txt
+++ b/test/config/CMakeLists.txt
@@ -7,4 +7,4 @@
 find_package (gflags REQUIRED)
 
 add_executable (foo main.cc)
-target_link_libraries (foo gflags)
+target_link_libraries (foo gflags::gflags)
diff --git a/test/gflags_declare_test.cc b/test/gflags_declare_test.cc
index 707bcc0..47d11c2 100644
--- a/test/gflags_declare_test.cc
+++ b/test/gflags_declare_test.cc
@@ -5,8 +5,8 @@
 
 int main(int argc, char **argv)
 {
-  gflags::SetUsageMessage("Test compilation and use of gflags_declare.h");
-  gflags::ParseCommandLineFlags(&argc, &argv, true);
+  GFLAGS_NAMESPACE::SetUsageMessage("Test compilation and use of gflags_declare.h");
+  GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
   print_message();
   return 0;
 }