google / s2geometry

Computational geometry and spatial indexing on the sphere
http://s2geometry.io/
Apache License 2.0
2.29k stars 303 forks source link

Don't forget to define macro S2_USE_GFLAGS when compiling s2geometry when your project that use s2 and also use gflags. #206

Closed jievince closed 2 years ago

jievince commented 2 years ago

My project uses gflags library, and when I try to include s2/s2polyline.h in one of my project's header file, I got many link errors like undefined reference to FLAGS_wal_buffer_size. It confuses me a lot because my project links well before I include s2polyline.h. Then I find s2polyline.h indirectly includes s2/base/commandlineflags.h:

#ifndef S2_BASE_COMMANDLINEFLAGS_H_
#define S2_BASE_COMMANDLINEFLAGS_H_

#ifdef S2_USE_GFLAGS

#include <gflags/gflags.h>

#else  // !defined(S2_USE_GFLAGS)

#include <string>

#include "s2/base/integral_types.h"

#define DEFINE_bool(name, default_value, description) \
  bool FLAGS_##name = default_value
#define DECLARE_bool(name) \
  extern bool FLAGS_##name

#define DEFINE_double(name, default_value, description) \
  double FLAGS_##name = default_value
#define DECLARE_double(name) \
  extern double FLAGS_##name

#define DEFINE_int32(name, default_value, description) \
  int32 FLAGS_##name = default_value
#define DECLARE_int32(name) \
  extern int32 FLAGS_##name

#define DEFINE_string(name, default_value, description) \
  std::string FLAGS_##name = default_value
#define DECLARE_string(name) \
  extern std::string FLAGS_##name

#endif  // !defined(S2_USE_GFLAGS)

#endif  // S2_BASE_COMMANDLINEFLAGS_H_

Because my project's header file indirectly includes this file, and I forget to define macro S2_USE_GFLAGS when compiling s2 library, so this file rewrites gflags related macro like DEFINE_bool in my project. And it cause the link error!

jievince commented 2 years ago

Seems this pr https://github.com/google/s2geometry/pull/96 has solves this problem