rcutils  master
C API providing common utilities and data structures.
visibility_control_macros.h
Go to the documentation of this file.
1 // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCUTILS__VISIBILITY_CONTROL_MACROS_H_
16 #define RCUTILS__VISIBILITY_CONTROL_MACROS_H_
17 
18 // Defines macros to express whether a symbol is localed, imported, or exported
19 //
20 // Those macros are compatible with GCC, clang, and Microsoft Visual C++. They
21 // can be used to enforce which symbols of a library are publicly accessible.
22 //
23 // RCUTILS_IMPORT, RCUTILS_EXPORT, and RCUTILS_LOCAL are respectively declaring
24 // an imported, exported, or local symbol.
25 // RCUTILS_LOCAL can be used directly. However, RCUTILS_IMPORT, and
26 // RCUTILS_EXPORT may not be used directly. Every project need to provide
27 // an additional header called `visibility_macros.h` containing:
28 //
29 // #ifdef <project>_BUILDING_DLL
30 // # define <project>_PUBLIC RCUTILS_EXPORT
31 // #else
32 // # define <project>_PUBLIC RCUTILS_IMPORT
33 // #endif // !<project>_BUILDING_DLL
34 // #define <project>_LOCAL RCUTILS_LOCAL
35 //
36 // ...where "<project>" has been replaced by the project name, such as
37 // "MY_PROJECT".
38 // Your project CMakeLists.txt should also contain the following statement:
39 //
40 // target_compile_definitions(<your library> PRIVATE "<project>_BUILDING_DLL")
41 //
42 // A public (exported) class should then be tagged as <project>_PUBLIC, whereas
43 // a non-public class should be tagged with <project>_LOCAL.
44 //
45 // See GCC documentation: https://gcc.gnu.org/wiki/Visibility
46 
47 #if defined(_MSC_VER) || defined(__CYGWIN__)
48 // Use the Windows syntax when compiling with Microsoft Visual C++.
49 //
50 // GCC on Windows also support this syntax. When compiling with cygwin,
51 // prefer using dllimport/dllexport are the semantic of those flags is closer
52 // to msvc++ behavior. See GCC documentation:
53 // https://gcc.gnu.org/onlinedocs/gcc/Microsoft-Windows-Function-Attributes.html
54 # define RCUTILS_IMPORT __declspec(dllimport)
55 # define RCUTILS_EXPORT __declspec(dllexport)
56 # define RCUTILS_LOCAL
57 #else // defined(_MSC_VER) || defined(__CYGWIN__)
58 // On Linux, use the GCC syntax. This syntax is understood by other compilers
59 // such as clang, icpc, and xlc++.
60 # define RCUTILS_IMPORT __attribute__ ((visibility("default")))
61 # define RCUTILS_EXPORT __attribute__ ((visibility("default")))
62 # define RCUTILS_LOCAL __attribute__ ((visibility("hidden")))
63 #endif // !defined(_MSC_VER) && !defined(__CYGWIN__)
64 
65 #endif // RCUTILS__VISIBILITY_CONTROL_MACROS_H_