Skip to content

Commit

Permalink
Add Framwork example
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Jul 7, 2024
1 parent 4f35c99 commit 3002be5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
69 changes: 69 additions & 0 deletions chapter16/framework_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions chapter16/framework_example/include/hello/hello.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#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_;
};
}
14 changes: 14 additions & 0 deletions chapter16/framework_example/info.plist.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>@MACOSX_BUNDLE_IDENTIFIER@</string>
<key>CFBundleName</key>
<string>@MACOSX_BUNDLE_NAME@</string>
<key>CFBundleVersion</key>
<string>@MACOSX_BUNDLE_VERSION@</string>
<key>CFBundleExecutable</key>
<string>@MACOSX_BUNDLE_EXECUTABLE@</string>
</dict>
</plist>
9 changes: 9 additions & 0 deletions chapter16/framework_example/src/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <hello/hello.hpp>

#include "internal.hpp"

namespace hello{
void Hello::greet() const {
details::print_impl(name_);
}
}
10 changes: 10 additions & 0 deletions chapter16/framework_example/src/internal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "internal.hpp"

#include <iostream>

namespace hello::details{
void print_impl(const std::string& name)
{
std::cout << "Hello " << name << " from a shared library\n";
}
}
9 changes: 9 additions & 0 deletions chapter16/framework_example/src/internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>

namespace hello::details{

void print_impl(const std::string& name);

}

0 comments on commit 3002be5

Please sign in to comment.