error "<glog/logging.h> was not included correctly." in including glog in c++ program

1k Views Asked by At

I have an error in including <glog/logging.h>.

My program is (test.cc):

#include <glog/logging.h>

int main(int argc, char* argv[]) {
    // Initialize Google’s logging library.
    google::InitGoogleLogging(argv[0]);

    // ...
    LOG(INFO) << "Found cookies";
}

only it!

And my CMakeLists.txt is:

cmake_minimum_required (VERSION 3.16)
project (myproj VERSION 1.0)

find_package (glog 0.6.0 REQUIRED)

add_executable (myapp test.cc)
target_link_libraries (myapp glog::glog)

Also errors are:

[build] In file included from /home/smh/Documents/projects/hist/hist/src/hist.cc:4:
[build] /usr/local/include/glog/logging.h:60:4: error: #error <glog/logging.h> was not included correctly. See the documention for how to consume the library.
[build]    60 | #  error <glog/logging.h> was not included correctly. See the documention for how to consume the library.
[build]       |    ^~~~~
[build] In file included from /usr/local/include/glog/logging.h:63,
[build]                  from /home/smh/Documents/projects/hist/hist/src/hist.cc:4:
[build] /usr/local/include/glog/flags.h:45:4: error: #error <glog/flags.h> was not included correctly. See the documention for how to consume the library.
[build]    45 | #  error <glog/flags.h> was not included correctly. See the documention for how to consume the library.
[build]       |    ^~~~~
[build] In file included from /usr/local/include/glog/logging.h:77,
[build]                  from /home/smh/Documents/projects/hist/hist/src/hist.cc:4:
[build] /usr/local/include/glog/log_severity.h:38:4: error: #error <glog/log_severity.h> was not included correctly. See the documention for how to consume the library.
[build]    38 | #  error <glog/log_severity.h> was not included correctly. See the documention for how to consume the library.
[build]       |    ^~~~~
[build] In file included from /usr/local/include/glog/logging.h:78,
[build]                  from /home/smh/Documents/projects/hist/hist/src/hist.cc:4:
[build] /usr/local/include/glog/vlog_is_on.h:71:4: error: #error <glog/vlog_is_on.h> was not included correctly. See the documention for how to consume the library.
[build]    71 | #  error <glog/vlog_is_on.h> was not included correctly. See the documention for how to consume the library.
[build]       |    ^~~~~
[build] In file included from /usr/local/include/glog/logging.h:63,
[build]                  from /home/smh/Documents/projects/hist/hist/src/hist.cc:4:
[build] /usr/local/include/glog/flags.h:105:1: error: ‘GLOG_EXPORT’ does not name a type
[build]   105 | DECLARE_int32(logemaillevel);
[build]       | ^~~~~~~~~~~~~
[build] /usr/local/include/glog/flags.h:105:1: error: ‘FLAGS_logemaillevel’ has not been declared in ‘fLI’
[build]   105 | DECLARE_int32(logemaillevel);
[build]       | ^~~~~~~~~~~~~
[build] /usr/local/include/glog/flags.h:106:1: error: ‘GLOG_EXPORT’ does not name a type
[build]   106 | DECLARE_int32(logcleansecs);
[build]       | ^~~~~~~~~~~~~
[build] /usr/local/include/glog/flags.h:106:1: error: ‘FLAGS_logcleansecs’ has not been declared in ‘fLI’
[build]   106 | DECLARE_int32(logcleansecs);
[build]       | ^~~~~~~~~~~~~
...

I installed glog from its README in github and all its tests were successful. text

And I installed this: text

Please help me.

2

There are 2 best solutions below

1
SHR On BEST ANSWER

It's looks like you are using it on Windows, without specify how to use the DLL or library. What is causing it is the following section in the header file:

#if !defined(GLOG_EXPORT) || !defined(GLOG_NO_EXPORT)
#  error <glog/logging.h> was not included correctly. See the documention for how to consume the library.
#endif

In other words: need to add a definition to CMake add_definitions(-DGLOG_NO_EXPORT) if you are using glog as a static lib, or add_definitions(-DGLOG_USE_GLOG_EXPORT) if glog is a DLL.

If you built glog by yourself, make sure you set the appropriate swiches.

0
maxirmx On

Unless you are building glog library the simplest solution is to compile with

#define GLOG_USE_GLOG_EXPORT

It will include pltform specific file generated during installation. This file will define GLOG_EXPORT and GLOG_NO_EXPORT as appropriate.