Skip to content

Commit eb4bbcf

Browse files
authored
Add support for installation with CMake (#100)
* rename dump.hpp -> cpp-dump.hpp * add dump.hpp for compatiblity * add #pragma once * rename cpp-dump.hpp -> cpp_dump.hpp * improve README.md * improve README.md * tweak * rename cpp_dump.hpp -> cpp-dump.hpp * hpp/ -> cpp-dump/hpp/ * fix table of contents * add comment about dump.hpp * improve README.md * improve README.md
1 parent 44fbb03 commit eb4bbcf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+371
-307
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ target_include_directories(cpp-dump INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_
1414
# Install
1515
include(GNUInstallDirs)
1616
install(
17-
FILES "dump.hpp"
18-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cpp-dump"
17+
FILES "cpp-dump.hpp"
18+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1919
)
2020
install(
21-
DIRECTORY "hpp"
22-
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/cpp-dump"
21+
DIRECTORY "cpp-dump"
22+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
2323
)
2424

2525
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" IS_TOP_LEVEL)

README.md

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Key points:
2828
- [20+ Manipulators to change the display style](#20-manipulators-to-change-the-display-style)
2929
- [Requirement](#requirement)
3030
- [Installation](#installation)
31+
- [Simplest solution](#simplest-solution)
32+
- [With CMake](#with-cmake)
33+
- [Run `cmake --install` to copy the headers to `/usr/local/include/` or equivalent](#run-cmake---install-to-copy-the-headers-to-usrlocalinclude-or-equivalent)
34+
- [Use `FetchContent`](#use-fetchcontent)
3135
- [Configuration (as needed)](#configuration-as-needed)
3236
- [Configuration options](#configuration-options)
3337
- [`max_line_width`](#max_line_width)
@@ -225,53 +229,107 @@ See [Formatting with manipulators](#formatting-with-manipulators) for details.
225229
226230
## Installation
227231
232+
### Simplest solution
233+
228234
```shell
229235
git clone https://github.com/philip82148/cpp-dump
230236
```
231237

232238
Then
233239

234240
```cpp
235-
#include "path/to/cpp-dump/dump.hpp"
241+
#include "path/to/cpp-dump/cpp-dump.hpp"
242+
```
243+
244+
### With CMake
245+
246+
#### Run `cmake --install` to copy the headers to `/usr/local/include/` or equivalent
247+
248+
```shell
249+
git clone https://github.com/philip82148/cpp-dump
250+
cd cpp-dump
251+
cmake -S . -B build # No configuration is needed because the library is header-only.
252+
sudo cmake --install build
253+
# (The 'cpp-dump' folder can be removed after this.)
254+
```
255+
256+
Then
257+
258+
```cpp
259+
#include <cpp-dump.hpp>
260+
```
261+
262+
#### Use `FetchContent`
263+
264+
`CMakeLists.txt`
265+
266+
```cmake
267+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Generate compile_commands.json (optional)
268+
269+
include(FetchContent)
270+
271+
# Fetch cpp-dump
272+
FetchContent_Declare(cpp-dump
273+
GIT_REPOSITORY https://github.com/philip82148/cpp-dump
274+
GIT_TAG main
275+
)
276+
FetchContent_MakeAvailable(cpp-dump)
277+
278+
# Link cpp-dump to your app
279+
target_link_libraries(MyApp PRIVATE cpp-dump)
280+
```
281+
282+
Then
283+
284+
```cpp
285+
#include <cpp-dump.hpp>
236286
```
237287

238288
## Configuration (as needed)
239289

240-
If you want to customize the library, use the `CPP_DUMP_SET_OPTION_GLOBAL()` macro:
290+
If you want to customize the library, you can write the configuration code as follows:
291+
292+
`custom-cpp-dump.hpp`
241293

242294
```cpp
243-
// You can also write this in a header file -----------------------------------
244295
#ifdef DEBUGGING
245-
#include "path/to/cpp-dump/dump.hpp"
296+
#include "path/to/cpp-dump/cpp-dump.hpp"
246297
namespace cp = cpp_dump;
298+
// You can use this in both a header file and a source file,
299+
// but make sure not to use it more than once for the same option.
247300
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);
301+
302+
// To ensure proper instantiation of templates,
303+
// include these in at least one translation unit where cpp_dump(...) prints each type.
304+
// One way is to write them in a header file and then include it wherever needed.
305+
CPP_DUMP_DEFINE_EXPORT_ENUM(my_enum, my_enum::a, my_enum::b, my_enum::c);
306+
CPP_DUMP_DEFINE_EXPORT_OBJECT(my_class, member1, member2());
307+
CPP_DUMP_DEFINE_EXPORT_OBJECT_GENERIC(member3, member4());
248308
#else
249309
#define cpp_dump(...)
310+
#define CPP_DUMP_SET_OPTION(...)
250311
#endif
251-
// You can also write this in a header file -----------------------------------
312+
```
313+
314+
`main.cpp`
315+
316+
```cpp
317+
#include "path/to/custom-cpp-dump.hpp"
252318

253319
int main() {
254-
// To be continued...
320+
cpp_dump(vec | cp::back());
255321
}
256322
```
257323

258324
If you want to configure the library within a function, use `CPP_DUMP_SET_OPTION()` instead.
259325

260326
```cpp
261-
// You can also write this in a header file -----------------------------------
262-
#ifdef DEBUGGING
263-
#include "path/to/cpp-dump/dump.hpp"
264-
namespace cp = cpp_dump;
265-
#else
266-
#define cpp_dump(...)
267-
#define CPP_DUMP_SET_OPTION(...)
268-
#endif
269-
// You can also write this in a header file -----------------------------------
327+
#include "path/to/custom-cpp-dump.hpp"
270328

271-
int main() {
272-
CPP_DUMP_SET_OPTION(max_line_width, 100);
273-
274-
// To be continued...
329+
void func() {
330+
CPP_DUMP_SET_OPTION(print_expr, false);
331+
cpp_dump(vec | cp::back());
332+
CPP_DUMP_SET_OPTION(print_expr, true);
275333
}
276334
```
277335

@@ -1003,7 +1061,7 @@ inline void cpp_dump::write_log(std::string_view output) {
10031061
10041062
```cpp
10051063
#ifdef DEFINED_ONLY_IN_LOCAL
1006-
#include "./cpp-dump/dump.hpp"
1064+
#include "./cpp-dump/cpp-dump.hpp"
10071065
#define dump(...) cpp_dump(__VA_ARGS__)
10081066
namespace cp = cpp_dump;
10091067
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 80);

0 commit comments

Comments
 (0)