Confusion regarding visibility_control for static libraries for Windows

Open
#2,747 2 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@mjcarroll is already working on this.

Since Feb 27, 2025.

Assessment

This issue has not been assessed yet.

Description

Looking at visibility_control.hpp (and all of these throughout the ROS2 codebase), it seems like it was never designed for static linkage on Windows. Say I don't have RCLCPP_BUILDING_LIBRARY in my preprocessor flags, RCLCPP_PUBLIC_TYPE will use __declspec(dllimport), which is probably not what you want during statically linking. Other libraries tend to just leave those defines blank --even the second example in the linked website in the header does that (https://gcc.gnu.org/wiki/Visibility).

Am I missing something fundamental here? C++ isn't my strong suite, but I'm trying to get static libraries instead of shared libraries/.dlls.

Thank you!

#ifndef RCLCPP__VISIBILITY_CONTROL_HPP_
#define RCLCPP__VISIBILITY_CONTROL_HPP_

// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
//     https://gcc.gnu.org/wiki/Visibility

#if defined _WIN32 || defined __CYGWIN__
  #ifdef __GNUC__
    #define RCLCPP_EXPORT __attribute__ ((dllexport))
    #define RCLCPP_IMPORT __attribute__ ((dllimport))
  #else
    #define RCLCPP_EXPORT __declspec(dllexport)
    #define RCLCPP_IMPORT __declspec(dllimport)
  #endif
  #ifdef RCLCPP_BUILDING_LIBRARY
    #define RCLCPP_PUBLIC RCLCPP_EXPORT
  #else
    #define RCLCPP_PUBLIC RCLCPP_IMPORT
  #endif
  #define RCLCPP_PUBLIC_TYPE RCLCPP_PUBLIC
  #define RCLCPP_LOCAL
#else
  #define RCLCPP_EXPORT __attribute__ ((visibility("default")))
  #define RCLCPP_IMPORT
  #if __GNUC__ >= 4
    #define RCLCPP_PUBLIC __attribute__ ((visibility("default")))
    #define RCLCPP_LOCAL  __attribute__ ((visibility("hidden")))
  #else
    #define RCLCPP_PUBLIC
    #define RCLCPP_LOCAL
  #endif
  #define RCLCPP_PUBLIC_TYPE
#endif

#endif  // RCLCPP__VISIBILITY_CONTROL_HPP_
Dominant language
C++
Stars
829
Forks
587
Avg merge
4d 6h
Merged PRs (30d)
30

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ros2/rclcpp

All issues in ros2/rclcpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.