cpp-embedded-rc
or RC
is a a command line tool and a library to generate
and embed binary resources in your application written in C++.
You will need conan and cmake.
The minimal conanfile.txt
you need looks like:
[requires]
cpp-embedded-rc/1.0.0@khdkhd/stable
[generators]
cmake
cmake_paths
Then your CMakeLists.txt
should look like:
cmake_minimum_required(VERSION 3.0)
project(YOUR_PROJECT CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
conan_basic_setup()
include(rc/generate) # include the function to generate resources
rc_generate(
ASSETS
NAME Assets
FILES
PATH_TO_ASSET1
PATH_TO_ASSET2
⋮
PATH_TO_ASSET1
)
add_executable(${PROJECT_NAME}
main.cpp
${ASSETS}
)
target_compile_features(${PROJECT_NAME}
PRIVATE
cxx_std_17
)
target_link_libraries(${PROJECT_NAME}
PRIVATE
${CONAN_LIBS}
)
rc-generate [OPTIONS] file [file...]
-h
,--help
- display help message and exit.-v
,--verbose
- enable verbose mode.-b
,--base arg
- set base directory value. The default value is the current working directory.-p
,--prefix arg
- set base directory value. The default value is/
.-n
,--name arg
- set the resource name. The default value isbinary_data
.-d
,--output-dir arg
- set output directory.
Each created resource is registered with a path.
This path is computed by joining the given prefix with the path of a file relative to the given base directory.
To be able to create embeddable resources for your application with CMake you
will have to include the module following rc/generate
.
The easiest way to accomplish this is to use the conan_paths
Conan
generator.
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
conan_basic_setup()
include(rc/generate)
rc_generate(OUTPUT_VAR
[PREFIX prefix_path]
[BASE basedir_path]
[NAME name]
FILES file1 [file2 ...]
)
OUTPUT_VAR
- the generated resource file path is stored in the given variable,PREFIX prefix_path
- the prefix path prepended to each registered resources,BASE basedir_path
- the resource pathes are relative to this path,NAME name
- use the create resource filename, and forRC_DECLARE
andRC_REGISTER
macro.
Before anything make sure you include the rc header.
#include <rc.hpp>
The following code examples assumed some that resources with name
SomeEmbeddedResources
and SomeOtherEmbeddedResources
have been generated ()
and linked with your application
(see Generate embeddable resources part for
more details).
Use RC_DECLARE
to declare some resources.
#include <rc.hpp>
RC_DECLARE(SomeEmbeddedResources)
RC_DECLARE(SomeOtherEmbeddedResources)
Once the resources are declared you will be able to register it by calling
RC_REGISTER
macro to register the declared resources. This step is required
to access resources afterwards.
int
main(int argc, char **argv) {
RC_REGISTER(SomeEmbeddedResources);
RC_REGISTER(SomeOtherEmbeddedResources);
// ...
return 0;
}
It checks if a resource exists for a given path.
bool exists(const std::string &path);
path
- the resource path to test.
true
- if and only if a resource has been registered for this path,false
- otherwise
It returns the path list of all registered resources.
std::vector<std::string> resources();
A std::vector<string>
.
khdkhd::rc::streambuf
is a std::basic_streambuf
whose associated character
sequence is an embedded resource.
Opens the resource at the given path.
khdkhd::rc::streambuf &open(const std::string &path);
path
- the path of the resource to be opened.
*this
.
Checks if the most recent call to open()
succeeded.
bool is_open() const;
Constructs new resource input stream.
khdkhd::rc::istream::istream() (1)
khdkhd::rc::istream::istream(const std::string &path) (2)
- (1) Default constructor: constructs a stream that is not associated with a resource.
- (2) First, performs the same steps as the default constructor, then
associates the stream with a resource by calling
khdkhd::rc::istream::open
.
path
- the path of the resource to be opened.
Opens and associates the resource with given path with the stream.
Calls setstate(failbit)
on failure.
khdkhd::rc::istream &khdkhd::rc::istream::open(const std::string &path)
path
- the path of the resource to be opened.
*this
.
Checks if the file stream has an associated resource.
khdkhd::rc::istream &khdkhd::rc::istream::is_open() const
true
if the file stream has an associated file,false
otherwise