Skip to content

Commit 5f2d058

Browse files
committed
Updated README.md:
* Reflecting `FetchContent` compatibility * Other changes to the usage section * Emphasized how aliasing standard library functions must be followed by the appropriate preprocessor definition on use (regards #67).
1 parent bda2f1f commit 5f2d058

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

README.md

+29-11
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,44 @@ The author of this fork was one of the latercomer bug-reporters-and-PR-authors;
3939

4040
## Using the `printf` library in your project
4141

42-
There are at least four ways to use this library:
42+
** Use involving CMake: **
4343

44-
1. Use CMake to configure, build and install the library. Then, in another CMake project, use `find_package(printf)` and make sure the install location is in the package search path.
45-
2. Use CMake to configure and build the library. You now have a library file (named `printf.a`, or `printf.so`, or `printf.dll` depending on your platform and choice of static vs dynamic linking), a header file, `printf.h`, and an optional extra header file `printf_config.h` with the build configuration details (usually unnecessary). In your project, if you include `printf.h` and link against the library file, you're all set: There are no dependencies.
46-
3. Copy `printf.c` and `printf.h` into your own project, and build them yourself. Note the various preprocessor options controlling the library's behavior! You will have to set them yourself, or accept with the default values (which are quite reasonable). Remember that the library requires compilation with the C99 language standard enabled.
47-
4. Include the contents of `printf.c` into your own code. This works well enough - whether it's a C or C++ file, and even within a namespace. You again need to consider the preprocessor options controlling behavior, and the language standard.
44+
1. Use CMake to configure, build and install the library. Then, in another CMake project, use `find_package(printf)` and make sure the library's install location is in CMake's package search path.
45+
2. Use CMake to configure and build the library. This results in the following file:
4846

49-
As mentioned earlier, and with all four options - using `printf()` or `vprintf()` will require implementing a `_putchar(char c)` function. If you like, you can have the library functions alias the standard library function names (e.g. have `snprintf()` really invoke `snprintf_()`) using a CMake configuration option or directly with an appropriate macro.
47+
* An object code library file (named `printf.a`, or `printf.so`, or `printf.dll` depending on your platform and choice of static vs dynamic linking)
48+
* A header file, `printf.h`
49+
* (Unnecessary) An optional extra header file `printf_config.h` with the build configuration details.
5050

51-
If you've started using the library in a publicly-available (FOSS or commercial) project, please consider emailing [@eyalroz](https://github.com/eyalroz), or open an [issue](https://github.com/eyalroz/printf/issues/), to announce this.
51+
Now, in your project, include `printf.h` and link against the library file, you're all set: There are no dependencies to satisfy or keep track of.
52+
3. Use CMake's `FetchContent` module to obtain the project source code and make it part of your own project's build, e.g.:
53+
```
54+
FetchContent_Declare(printf_library
55+
GIT_REPOSITORY https://github.com/eyalroz/printf.git
56+
GIT_TAG v12.34.45 # Replace this with a real available version
57+
)
58+
FetchContent_MakeAvailable(printf_library)
59+
```
60+
4. Copy `printf.c` and `printf.h` into your own project, and compile the source however you see fit. Remember that the library requires compilation with the C99 language standard enabled.
61+
5. Include the contents of `printf.c` into your own code. This works well enough - whether it's a C or C++ file, and even within a namespace. You again need to consider the language standard.
62+
63+
Whichever way you choose to use the library:
64+
65+
* You can have this library stand-in for the C standard library's `printf()` family of functions, e.g. provide `snprintf()` instead of `snprintf_()`, by setting an appropriate [preprocessor definition](#cmake-options-and-preprocessor-definitions) during compilation and use.
66+
* Speaking of the preprocessor definition [preprocessor definitions](#cmake-options-and-preprocessor-definitions) which affect the library's behavior - you have to be consistent in their choice when building and when using the library. (The easiest way to do that is just not to change any of them and accept the reasonable defaults.)
67+
* Two of the functions --- `printf_()` and `vprintf_()` --- will only be usable if you implement a `_putchar(char c)` function for them to use.
68+
* **Avoid `sprintf()` in favor of `snprintf()` for safety and security** - and that goes for the standard C library `sprintf()` as well:. `sprintf()` is unaware of the amount of memory allocated for the string it writes into, and will "happily" overflow your buffer; instead of calling it, pass your buffer size to `snprintf()` - and avoid overflow.
69+
70+
Finally, if you've started using the library in a publicly-available (FOSS or commercial) project, please consider emailing [@eyalroz](https://github.com/eyalroz), or open an [issue](https://github.com/eyalroz/printf/issues/), to announce this.
5271

53-
**Important note: The `sprintf` function is dangerous and insecure, and should be avoided in favor of `snprintf`:** `sprintf()` is unaware of the amount of memory allocated for the printed string, and will "happily" overflow your buffer; instead, pass your buffer size to `snprintf()` - and avoid overflow.
5472

5573
### CMake options and preprocessor definitions
5674

5775
Options used both in CMake and in the library source code via a preprocessor define:
5876

5977
| Option name | Default | Description |
6078
|----------------------------------------|---------|--------------|
61-
| PRINTF_ALIAS_STANDARD_FUNCTION_NAMES | NO | Alias the standard library function names (`printf()`, `sprintf()` etc.) to the library's functions |
79+
| PRINTF_ALIAS_STANDARD_FUNCTION_NAMES | NO | Alias the standard library function names (`printf()`, `sprintf()` etc.) to the library's functions.<br>**Note:** If you build the library with this option turned on, you must also have written<br>`#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1`<br>before including the `printf.h` header. |
6280
| PRINTF_INTEGER_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. |
6381
| PRINTF_DECIMAL_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack. |
6482
| PRINTF_DEFAULT_FLOAT_PRECISION | 6 | Define the default floating point precision|
@@ -228,9 +246,9 @@ The following assumes Marco Paland's original repository remains mostly-inactive
228246
5. Add new checks or test-cases to the test suite - both for any problems you have identified and for any new functionality you have introduced.
229247
4. Commit your changes (`git commit -a -m "Added some feature"`)
230248
5. Publish the branch (`git push origin my-new-feature`)
231-
6. Create a new pull request against this repository.
249+
6. Create a new pull request against this repository. Note: Please don't create a PR without a related issue.
232250

233-
I will try to attend to PRs promptly.
251+
I try to attend to issues and PRs promptly.
234252

235253

236254
## License

0 commit comments

Comments
 (0)