在 Windows 上将 glog 与 FetchContent 结合使用

问题描述 投票:0回答:1

我正在尝试编译

#include <glog/logging.h>

int main(int argc, char **argv) {
    ::google::InitGoogleLogging(argv[0]);
    LOG(ERROR) << "Test";
}

使用我的 Visual Studio 17 2022

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(asdf)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include(FetchContent)
FetchContent_Declare(
  glog
  GIT_REPOSITORY https://github.com/google/glog.git
  GIT_TAG "v0.7.0"
)
FetchContent_MakeAvailable(glog)
add_executable(main src/Main.cpp)
target_link_libraries(main glog::glog)

cmake ..
给出了一堆警告

cmake ..
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.26100.
-- Could NOT find GTest (missing: GTest_DIR)
CMake Warning at build/_deps/glog-src/CMakeLists.txt:77 (find_package):
  By not providing "Findgflags.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "gflags", but
  CMake did not find one.

  Could not find a package configuration file provided by "gflags" (requested
  version 2.2.2) with any of the following names:

    gflagsConfig.cmake
    gflags-config.cmake

  Add the installation prefix of "gflags" to CMAKE_PREFIX_PATH or set
  "gflags_DIR" to a directory containing one of the above files.  If "gflags"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Could NOT find Unwind (missing: Unwind_INCLUDE_DIR Unwind_LIBRARY)
-- Configuring done (2.3s)
-- Generating done (0.3s)

我也尝试使用 FetchContent 添加 gflags 和 gtest,但不起作用。我尝试编译 gflags 和 gtest 并使用

set(gflags_DIR "${CMAKE_SOURCE_DIR}/ext/gflags/_build/")
等设置目录,但它不起作用:编译 main.cpp 时收到这些警告

\glog_test\build\_deps\glog-src\src\glog\logging.h(113,7): warning C4251: "google::LogMessageTime::timestamp_": "std::chrono::time_point<std::chrono::system_clock,std::chrono::duration<std::chrono::system_clock::rep,std::chrono::system_clock::period>>" requires a DLL interface that clients of "google::LogMessageTime" use [\glog_test\build\_deps\glog-build\glog_internal.vcxproj]
\glog_test\build\_deps\glog-src\src\flags.cc(157,1): warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

生成的 main.exe 立即崩溃。我还尝试使用 .lib 文件手动编译

cl .\src\Main.cpp /I".\ext\glog\src\" /link /LIBPATH:".\ext\glog\build\Release\" glog.lib

Main.cpp
.\ext\glog\src\glog/logging.h(60): fatal error C1189: #error:  <glog/logging.h> was not included correctly. See the documentation for how to consume the library.

我做错了什么?

cmake googletest glog gflags
1个回答
0
投票

使用标志

-DWITH_GTEST=OFF -DWITH_GFLAGS=OFF
运行 cmake 或在
FetchContent_MakeAvailable

之前添加
set (WITH_GTEST, OFF)
set (WITH_GFLAGS, OFF)
© www.soinside.com 2019 - 2024. All rights reserved.