This is my first time asking a question here, as I'm usually able to find answers in previous posts, but I can't find any information on this topic.
I'm trying to write C/C++ header files that can be reused between projects. One of my headers uses <math.h>
(deprecated in C++, so it uses <cmath>
instead). When I compile a C++ program to test the header, it works perfectly. When I compile a C program to test the header, the linker rightfully throws a fit unless "libm.a" is linked ("-l m" in most compilers). I'm not worried about myself forgetting to link the library, but I plan on making these headers available for public use, and I'd like to print a more helpful error message than "undefined reference". Here's my question, is there any way for me to use something akin to preprocessor directives to check if a specific library is linked properly when building the executable so I can throw my own error message?
Source code: https://github.com/LimikEcho/rlx/blob/main/itosa.h
I've tried checking if specific macros are defined (from both <math.h>
and <math.c>
). If I check a macro defined in <math.h>
, it returns 1
regardless of linking "libm.a". If I check a macro defined in <math.h>
, it returns 0
regardless of linking "libm.a". I understand why that is, but it was worth a shot.
Edit: People seem to be missing the point of this, so let me reiterate. I want to know if it's possible to influence the error handling of the linker from a header, that way whenever it's used by third parties, they can be accurately warned about missing a specific library. I want to display undefined reference errors as something like undefined reference to 'example_function', did you forget to link 'libm.a'?