Skip to content

Commit

Permalink
Better handling for export headers (#447)
Browse files Browse the repository at this point in the history
* Better handling for export headers

* More refinement on export headers.

* Add comment to explain export macros.

* Minor change on comment
  • Loading branch information
tongyuantongyu authored Dec 17, 2020
1 parent 2b7f04e commit 291a67a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,16 @@ target_include_directories(avif
PUBLIC $<BUILD_INTERFACE:${libavif_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE ${AVIF_PLATFORM_INCLUDES} ${AVIF_CODEC_INCLUDES})
set(AVIF_PKG_CONFIG_EXTRA_CFLAGS "")
if(BUILD_SHARED_LIBS)
target_compile_definitions(avif PRIVATE -DAVIF_BUILDING_SHARED_LIBS)
target_compile_definitions(avif PUBLIC AVIF_DLL)
target_compile_definitions(avif PRIVATE AVIF_BUILDING_SHARED_LIBS)
set(AVIF_PKG_CONFIG_EXTRA_CFLAGS " -DAVIF_DLL")
if(AVIF_LOCAL_LIBGAV1)
set_target_properties(avif PROPERTIES LINKER_LANGUAGE "CXX")
endif()
endif()


option(AVIF_BUILD_EXAMPLES "Build avif Examples." OFF)
if(AVIF_BUILD_EXAMPLES)
set(AVIF_EXAMPLES
Expand Down
47 changes: 35 additions & 12 deletions include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,46 @@
#include <stddef.h>
#include <stdint.h>

#ifndef AVIF_API
#if defined(AVIF_BUILDING_SHARED_LIBS)
#ifdef __cplusplus
extern "C" {
#endif

// ---------------------------------------------------------------------------
// Export macros

// AVIF_BUILDING_SHARED_LIBS should only be defined when libavif is being built
// as a shared library.
// AVIF_DLL should be defined if libavif is a shared library. If you are using
// libavif as CMake dependency, through CMake package config file or
// through pkg-config, this is defined automatically.
//
// Here's what AVIF_API will be defined as in shared build:
// | | Windows | Unix |
// | Build | __declspec(dllexport) | __attribute__((visibility("default"))) |
// | Use | __declspec(dllimport) | |
//
// For static build, AVIF_API is always defined as nothing.

#if defined(_WIN32)
#define AVIF_API __declspec(dllexport)
#define AVIF_HELPER_EXPORT __declspec(dllexport)
#define AVIF_HELPER_IMPORT __declspec(dllimport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#define AVIF_API __attribute__((visibility("default")))
#define AVIF_HELPER_EXPORT __attribute__((visibility("default")))
#define AVIF_HELPER_IMPORT
#else
#define AVIF_API
#endif // if defined(_WIN32)
#define AVIF_HELPER_EXPORT
#define AVIF_HELPER_IMPORT
#endif

#if defined(AVIF_DLL)
#if defined(AVIF_BUILDING_SHARED_LIBS)
#define AVIF_API AVIF_HELPER_EXPORT
#else
#define AVIF_API
#define AVIF_API AVIF_HELPER_IMPORT
#endif // if defined(AVIF_BUILDING_SHARED_LIBS)
#endif // ifndef AVIF_API

#ifdef __cplusplus
extern "C" {
#endif
#else
#define AVIF_API
#endif //if defined(AVIF_DLL)

// ---------------------------------------------------------------------------
// Constants
Expand Down
2 changes: 1 addition & 1 deletion libavif.pc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Name: @PROJECT_NAME@
Description: Library for encoding and decoding .avif files
Version: @PROJECT_VERSION@
Libs: -L${libdir} -lavif
Cflags: -I${includedir}
Cflags: -I${includedir}@AVIF_PKG_CONFIG_EXTRA_CFLAGS@

0 comments on commit 291a67a

Please sign in to comment.