From 3002be5b423aab44611c048afb85ff08a5071bb3 Mon Sep 17 00:00:00 2001 From: Dominik Berner Date: Sun, 7 Jul 2024 10:12:30 +0000 Subject: [PATCH] Add Framwork example --- chapter16/framework_example/CMakeLists.txt | 69 +++++++++++++++++++ .../framework_example/include/hello/hello.hpp | 17 +++++ chapter16/framework_example/info.plist.in | 14 ++++ chapter16/framework_example/src/hello.cpp | 9 +++ chapter16/framework_example/src/internal.cpp | 10 +++ chapter16/framework_example/src/internal.hpp | 9 +++ 6 files changed, 128 insertions(+) create mode 100644 chapter16/framework_example/CMakeLists.txt create mode 100644 chapter16/framework_example/include/hello/hello.hpp create mode 100644 chapter16/framework_example/info.plist.in create mode 100644 chapter16/framework_example/src/hello.cpp create mode 100644 chapter16/framework_example/src/internal.cpp create mode 100644 chapter16/framework_example/src/internal.hpp diff --git a/chapter16/framework_example/CMakeLists.txt b/chapter16/framework_example/CMakeLists.txt new file mode 100644 index 0000000..9eb0cc6 --- /dev/null +++ b/chapter16/framework_example/CMakeLists.txt @@ -0,0 +1,69 @@ +# +# SPDX-License-Identifier: MIT + +cmake_minimum_required(VERSION 3.25) + +project( + ch16_framework_example + VERSION 1.0.0 + DESCRIPTION + "A simple C++ project to demonstrate creating executables and libraries in CMake" + LANGUAGES CXX +) + +# add the library target +add_library(ch16_framework_example SHARED) + +# set properties for the target. VERSION set the library version to the project +# version * SOVERSION set the compatibility version for the library to the +# major number of the version +set_target_properties( + ch16_framework_example + PROPERTIES FRAMEWORK TRUE + FRAMEWORK_VERSION 1 + PUBLIC_HEADER include/hello.hpp + PRIVATE_HEADER src/internal.hpp + MACOSX_FRAMEWORK_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in" +) + +# add sources to the library target +target_sources( + ch16_framework_example + PRIVATE src/hello.cpp src/internal.cpp +) + +# set the include directories +target_include_directories( + ch16_framework_example + PRIVATE src/ch16_framework_example + PUBLIC include +) + +# if using limited visibility, set CXX_VISIBILILTY_PRESET to "hidden" +include(GenerateExportHeader) +set_property( + TARGET ch16_framework_example + PROPERTY CXX_VISIBILITY_PRESET "hidden" +) +# Hide inlined functions by default, reducing the size of the library +set_property( + TARGET ch16_framework_example + PROPERTY VISIBILITY_INLINES_HIDDEN TRUE +) +# this command generates a header file in the CMAKE_CURRENT_BINARY_DIR which +# sets the visibility attributes according to the compiler settings +generate_export_header( + ch16_framework_example + EXPORT_FILE_NAME + export/hello/export_hello.hpp +) +# Add CMAKE_CURRENT_BINARY_DIR to the include path so the generated header can +# be found +target_include_directories( + ch16_framework_example + PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/export" +) + +# An alternative but not recomended way export the symbols is to use +# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) Which exports all dll symbols by +# default diff --git a/chapter16/framework_example/include/hello/hello.hpp b/chapter16/framework_example/include/hello/hello.hpp new file mode 100644 index 0000000..9867549 --- /dev/null +++ b/chapter16/framework_example/include/hello/hello.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include "hello/export_hello.hpp" + + +namespace hello{ + /// Example class that is explicitly exported into a dll + class CH3_HELLO_SHARED_EXPORT Hello { + public: + Hello(const std::string& name) : name_{name} {} + + void greet() const; + private: + const std::string name_; + }; +} \ No newline at end of file diff --git a/chapter16/framework_example/info.plist.in b/chapter16/framework_example/info.plist.in new file mode 100644 index 0000000..87b9df5 --- /dev/null +++ b/chapter16/framework_example/info.plist.in @@ -0,0 +1,14 @@ + + + + + CFBundleIdentifier + @MACOSX_BUNDLE_IDENTIFIER@ + CFBundleName + @MACOSX_BUNDLE_NAME@ + CFBundleVersion + @MACOSX_BUNDLE_VERSION@ + CFBundleExecutable + @MACOSX_BUNDLE_EXECUTABLE@ + + diff --git a/chapter16/framework_example/src/hello.cpp b/chapter16/framework_example/src/hello.cpp new file mode 100644 index 0000000..c94ad58 --- /dev/null +++ b/chapter16/framework_example/src/hello.cpp @@ -0,0 +1,9 @@ +#include + +#include "internal.hpp" + +namespace hello{ + void Hello::greet() const { + details::print_impl(name_); + } +} \ No newline at end of file diff --git a/chapter16/framework_example/src/internal.cpp b/chapter16/framework_example/src/internal.cpp new file mode 100644 index 0000000..cc8faf9 --- /dev/null +++ b/chapter16/framework_example/src/internal.cpp @@ -0,0 +1,10 @@ +#include "internal.hpp" + +#include + +namespace hello::details{ + void print_impl(const std::string& name) + { + std::cout << "Hello " << name << " from a shared library\n"; + } +} \ No newline at end of file diff --git a/chapter16/framework_example/src/internal.hpp b/chapter16/framework_example/src/internal.hpp new file mode 100644 index 0000000..9b39759 --- /dev/null +++ b/chapter16/framework_example/src/internal.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace hello::details{ + + void print_impl(const std::string& name); + +} \ No newline at end of file