You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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).
Copy file name to clipboardExpand all lines: README.md
+29-11
Original file line number
Diff line number
Diff line change
@@ -39,26 +39,44 @@ The author of this fork was one of the latercomer bug-reporters-and-PR-authors;
39
39
40
40
## Using the `printf` library in your project
41
41
42
-
There are at least four ways to use this library:
42
+
** Use involving CMake: **
43
43
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:
48
46
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.
50
50
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.:
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.
52
71
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.
54
72
55
73
### CMake options and preprocessor definitions
56
74
57
75
Options used both in CMake and in the library source code via a preprocessor define:
| 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.|
62
80
| 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. |
63
81
| 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. |
64
82
| 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
228
246
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.
229
247
4. Commit your changes (`git commit -a -m "Added some feature"`)
230
248
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.
0 commit comments