blob: f1f829ca01ce6e8979d591b00e5d814457130532 [file] [log] [blame] [view]
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -04001# ndn-cxx examples
Alexander Afanasyev28d0d942015-01-04 14:52:19 -08002
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -04003By default, the examples in `examples/` folder are not built. To enable them, use
Alexander Afanasyev28d0d942015-01-04 14:52:19 -08004`--with-examples` configure option. For example:
5
6 ./waf configure --with-examples
7 ./waf
8
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -04009There are two ways to add new examples, depending on their complexity.
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080010
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -0400111. **Examples with a single translation unit**
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080012
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040013 For simple examples that have a single translation unit, the `.cpp` file can be directly put
14 in the `examples/` folder and it will be automatically compiled on the next run of `./waf`.
15 The name of the compiled executable will be determined by the base name of the `.cpp` file.
16 For instance, `examples/foo.cpp` will be compiled into an executable named `foo` inside the
17 `build/examples` folder:
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080018
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040019 echo 'int main() { return 0; }' > examples/foo.cpp
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080020 ./waf
21 # ... Compiling examples/foo.cpp
22 # ... Linking build/examples/foo
23
24 # To run the example
25 ./build/examples/foo
26
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -0400272. **Examples with multiple translation units**
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080028
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040029 For more complex examples that contain multiple translation units, one can use
30 the following directory structure:
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080031
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040032 - Create a directory under the `examples/` folder (e.g., `examples/bar`). The name of this
33 directory will determine the name of the compiled executable (`build/examples/bar/bar`).
34 - Place any number of translation units (e.g., `examples/bar/a.cpp`, `examples/bar/b.cpp`,
35 ...) in this directory. All `.cpp` files in this directory will be compiled and linked
36 together to produce the binary executable of the example. One of the .cpp files must
37 contain the `main()` function.
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080038
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040039 For example:
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080040
41 mkdir examples/bar
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040042 echo 'int bar(); int main() { return bar(); }' > examples/bar/a.cpp
43 echo 'int bar() { return 10; }' > examples/bar/b.cpp
Alexander Afanasyev28d0d942015-01-04 14:52:19 -080044 ./waf
45 # ... Compiling examples/bar/a.cpp
46 # ... Compiling examples/bar/b.cpp
47 # ... Linking build/examples/bar/bar
48
49 # To run the example
50 ./build/examples/bar/bar
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040051
52## Security configuration for example apps
53
54In order for the ``consumer`` example app to be able to properly authenticate data packets
55created by the ``producer`` app, you must configure the following parameters.
56
571. Generate example trust anchor:
58
59 ndnsec key-gen /example
60 ndnsec cert-dump -i /example > example-trust-anchor.cert
61
622. Create a key for the producer and sign it with the example trust anchor:
63
64 ndnsec key-gen /example/testApp
65 ndnsec sign-req /example/testApp | ndnsec cert-gen -s /example -i example | ndnsec cert-install -