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