7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GenerateExportHeader: visibilityの制御用ヘッダを自動的に生成する

Last updated at Posted at 2014-12-06

c++でdllを作成する際に、FOO_EXPORTのようなマクロを定義して、dllimport と dllexport を切り替えて使用するケースがある。(このへん正直あまりよくわかっていないのだけど・・・・)

GenerateExportHeaderモジュールを使用すると、このマクロを定義するファイルを自動的に生成できる。


include(GenerateExportHeader)
add_library(foo SHARED foo.cc)
generate_export_header(foo)

上記を実行すると、ビルドツリーに foo_export.h が生成されて、以下のマクロが定義される。

  • FOO_EXPORT
  • FOO_NO_EXPORT
  • FOO_DEPRECATED
  • FOO_DEPRECATED_EXPORT
  • FOO_DEPRECATED_NO_EXPORT

Linux+gcc 環境では下記のファイルが生成された


# ifndef FOO_EXPORT_H
# define FOO_EXPORT_H

# ifdef FOO_STATIC_DEFINE
#  define FOO_EXPORT
#  define FOO_NO_EXPORT
# else
#  ifndef FOO_EXPORT
#    ifdef foo_EXPORTS
        /* We are building this library */
#      define FOO_EXPORT __attribute__((visibility("default")))
#    else
        /* We are using this library */
#      define FOO_EXPORT __attribute__((visibility("default")))
#    endif
#  endif

#  ifndef FOO_NO_EXPORT
#    define FOO_NO_EXPORT __attribute__((visibility("hidden")))
#  endif
# endif

# ifndef FOO_DEPRECATED
#  define FOO_DEPRECATED __attribute__ ((__deprecated__))
#  define FOO_DEPRECATED_EXPORT FOO_EXPORT __attribute__ ((__deprecated__))
#  define FOO_DEPRECATED_NO_EXPORT FOO_NO_EXPORT __attribute__ ((__deprecated__))
# endif

あとは、ライブラリのソースファイルで生成されたファイルをインクルードして、さらにインストール対象にファイルを含めればいい。

細かいオプションは下記を参照すること。
http://www.cmake.org/cmake/help/v3.0/module/GenerateExportHeader.html

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?