Skip to content

Commit c4a1187

Browse files
committed
Add minimum working tinyopt example
1 parent 3c6b448 commit c4a1187

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required (VERSION 3.5)
2+
3+
project(tinyopt-example VERSION 0.0.1
4+
DESCRIPTION "A Tinyopt Example Project"
5+
LANGUAGES CXX)
6+
7+
# Compiler flags
8+
set(CMAKE_CXX_STANDARD 20)
9+
10+
# Import Eigen (Mandatory)
11+
find_package(Eigen3 REQUIRED NO_MODULE)
12+
13+
# Find Tinyopt, fetch it if not found
14+
find_package(Tinyopt)
15+
16+
if (NOT Tinyopt_FOUND)
17+
message("Tinyopt not found locally, fetching it")
18+
include(FetchContent)
19+
FetchContent_Declare(
20+
tinyopt
21+
GIT_REPOSITORY https://github.com/julien-michot/tinyopt.git
22+
GIT_TAG main
23+
GIT_SHALLOW TRUE
24+
GIT_PROGRESS TRUE
25+
)
26+
set(BUILD_TINYOPT_TESTS OFF)
27+
FetchContent_MakeAvailable(tinyopt)
28+
endif()
29+
30+
# Header-only library
31+
add_executable(example src/example.cpp)
32+
target_link_libraries(example PUBLIC Eigen3::Eigen tinyopt)

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
1+
2+
![Tinyopt-example Builds](https://github.com/julien-michot/tinyopt-example/actions/workflows/build.yml/badge.svg)
3+
14
# tinyopt-example
25
Minimal Tinyopt project
6+
7+
## CMakeLists.txt
8+
9+
```cmake
10+
set(CMAKE_CXX_STANDARD 20)
11+
12+
find_package(Eigen3 REQUIRED NO_MODULE)
13+
find_package(Tinyopt)
14+
15+
add_executable(example src/example.cpp)
16+
target_link_libraries(example PUBLIC Eigen3::Eigen tinyopt)
17+
```
18+
19+
20+
## Example.cpp
21+
22+
Here is an example of usage of 'tinyopt' where we compute the square root of 2.
23+
24+
```cpp
25+
#include <tinyopt/tinyopt.h>
26+
27+
int main(int, char **argv) {
28+
double x = 1;
29+
tinyopt::Optimize(x, [](const auto &x) { return x * x - 2.0; });
30+
std::cout << "What's √2 already?\n" << x << "\n";
31+
return 0;
32+
}
33+
```
34+
35+
### Building
36+
37+
```shell
38+
git clone https://github.com/julien-michot/tinyopt-example
39+
cd tinyopt-example && mkdir build && cd build
40+
cmake ..
41+
make -j
42+
```
43+
44+
### Running
45+
46+
```shell
47+
./example
48+
```

src/example.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// This is free and unencumbered software released into the public domain.
2+
/// Example of usage of 'tinyopt' and compute the square root of 2.
3+
4+
#include <tinyopt/tinyopt.h>
5+
6+
int main(int, char **argv) {
7+
8+
double x = 1;
9+
auto loss = [](const auto &x) { return x * x - 2.0; };
10+
11+
tinyopt::Options options;
12+
const auto &out = tinyopt::Optimize(x, loss, options);
13+
14+
std::cout << "What's √2 already?\n" << x << "\n";
15+
return 0;
16+
}

0 commit comments

Comments
 (0)