Call of Guitar
Call of Guitar

Reputation: 73

Using glog with FetchContent on Windows

I'm trying to compile

#include <glog/logging.h>

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

with Visual Studio 17 2022 using my 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 .. gives a bunch of warnings

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)

I've tried adding gflags and gtest with FetchContent as well, doesn't work. I tried compiling gflags and gtest and setting the directories with set(gflags_DIR "${CMAKE_SOURCE_DIR}/ext/gflags/_build/") and such, but it doesn't work: I get these warnings when compiling 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.

and the resulting main.exe crashes instantly. I also tried manually compiling with the .lib file

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.

What am I doing wrong?

Upvotes: 0

Views: 98

Answers (2)

Call of Guitar
Call of Guitar

Reputation: 73

For anyone curious, I got it to work by using conan. I followed conans documentation and created this conanfile.txt

[requires]
glog/0.7.1

[generators]
CMakeDeps
CMakeToolchain

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(GlogExample)

find_package(glog REQUIRED)

add_executable(main src/Main.cpp)
target_link_libraries(main glog::glog)

And executed:

conan profile detect --force
conan install . --output-folder=build --build=missing
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"
cmake --build . --config Release
.\Release\main.exe
E20241024 18:54:07.143535 20956 Main.cpp:5] Test

Package managers are great :)

Upvotes: 0

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38991

Run cmake with the flags -DWITH_GTEST=OFF -DWITH_GFLAGS=OFF or add before FetchContent_MakeAvailable

set (WITH_GTEST, OFF)
set (WITH_GFLAGS, OFF)

Upvotes: 0

Related Questions