diff --git a/common/BUILD.bazel b/common/BUILD.bazel index 3b62daf1d457..ad0c09cb3244 100644 --- a/common/BUILD.bazel +++ b/common/BUILD.bazel @@ -30,6 +30,7 @@ drake_cc_package_library( ":diagnostic_policy", ":double", ":drake_bool", + ":drake_export", ":drake_path", ":dummy_value", ":essential", @@ -190,6 +191,11 @@ drake_cc_library( # Miscellaneous utilities. +drake_cc_library( + name = "drake_export", + hdrs = ["drake_export.h"], +) + drake_cc_library( name = "identifier", srcs = ["identifier.cc"], diff --git a/common/drake_export.h b/common/drake_export.h new file mode 100644 index 000000000000..d42cc9c2a36e --- /dev/null +++ b/common/drake_export.h @@ -0,0 +1,39 @@ +#pragma once + +/** DRAKE_NO_EXPORT sets C++ code to use hidden linker visibility. + +Hidden visibility is appropriate for code that will be completely invisible to +users, e.g., for header files that are bazel-private, not installed, and only +used as implementation_deps. + +This macro is most useful when Drake code includes externals that themselves +have hidden linker visibility and compilers complain about mismatched +visibility attributes. + +For example, to un-export all classes and functions in a namespace: +
+namespace internal DRAKE_NO_EXPORT { +class Foo { + // ... +}; +} // namespace internal ++ +To un-export just one class: +
+namespace internal { +class DRAKE_NO_EXPORT Foo { + // ... +}; +} // namespace internal ++ +To un-export just one function: +
+DRAKE_NO_EXPORT void CalcFoo(double arg) { ... } ++ +For the related CMake module, see: +https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html +*/ +#define DRAKE_NO_EXPORT __attribute__((visibility("hidden"))) diff --git a/geometry/BUILD.bazel b/geometry/BUILD.bazel index 03545843bdbc..8befa9cea120 100644 --- a/geometry/BUILD.bazel +++ b/geometry/BUILD.bazel @@ -466,6 +466,7 @@ drake_cc_library( "//perception:point_cloud", ], deps = [ + "//common:drake_export", "//common:find_resource", "//common:scope_exit", "//common:unused", diff --git a/geometry/meshcat.cc b/geometry/meshcat.cc index 570acd1ae78b..a9e0154acc9f 100644 --- a/geometry/meshcat.cc +++ b/geometry/meshcat.cc @@ -24,6 +24,7 @@ #include