I'm creating a header only library and I wanted to check if the user has defined <string.h> so that I can use memcpy. I read online about how libraries like stdio have guard macros, but I couldn't find one for string.h. Any ideas? Or is there a way just to see if memcpy is a function?
Asked
Active
Viewed 405 times
0
-
5Why wouldn't you just `#include
` for yourself? Or just forward declare `void *memcpy(void *, const void *, size_t);` at the top of the source file you need it in? – selbie Apr 24 '21 at 17:06 -
1I guess I've just never heard of anyone having code paths that operated conditionally based on what the caller included. Is this a "header only" library you are putting together? – selbie Apr 24 '21 at 17:09
-
1@selbie yeah, it is a header only a library, so I my reasoning was that I should keep it as light as possible. – user15194465 Apr 24 '21 at 17:11
-
1**Any ideas?** simply `#include
`. There will be no problem if users of your library also include it on their own. **Or is there a way just to see if memcpy is a function?** No, not by the pre-processor. – pmg Apr 24 '21 at 17:23 -
@pmg Alright I see. I'll just include
. Thanks! – user15194465 Apr 24 '21 at 17:25 -
2Aside from my aversion to header only libraries, I don't agree with your reasoning. This is an over-optimization. Probably the best hybrid solution you can do is to just use memcpy without explicitly adding a `#include
`. Any developer worth his salt that includes your library will figure it out if he gets an error saying memcpy is undeclared. None of this changes my opinion that you are over-optimizing and only making your code harder to maintain. **Just include – selbie Apr 24 '21 at 17:25** -
@selbie yeah I got it. Thanks again! – user15194465 Apr 24 '21 at 17:37
-
@selbie I wouldn't declare it yourself, as there might be subtleties you get different (and thus wrong). Just including it (yet again) is right though. – Deduplicator Apr 24 '21 at 18:43
-
@Deduplicator - agree. – selbie Apr 24 '21 at 18:52
-
can you pls untick my answer – Apr 24 '21 at 22:44
-
@CEPB Yes, I just did – user15194465 Apr 24 '21 at 23:06
2 Answers
2
You can portably tell if string.h
has not been included.
Per 7.24.1 String function conventions, paragraph 1 of the (draft) C11 standard:
The header
<string.h>
declares one type and several functions, and defines one macro useful for manipulating arrays of character type and other objects treated as arrays of character type. The type issize_t
and the macro isNULL
...
If NULL
is not defined, then the user could not have included string.h
prior to including your header(s).
I see no portable way of definitively determining if string.h
has been included.

Andrew Henle
- 32,625
- 3
- 24
- 56
-
WOW good one, but hold on what do you mean (draft), that means its not in C11 standard? – Apr 24 '21 at 17:42
-
1@CEPB That's a link to a copy of the final draft C11 standard - one of the few available. Links to the actual standard don't exist AFAICT - copies of the actual standard are not free and I suspect they're licensed/sold with restrictions against posting the contents. As that's the *final* draft, it's likely identical to the actual C11 standard but I haven't done any real checking. AFAIK no one has noted any differences from the actual C11 standard. – Andrew Henle Apr 24 '21 at 17:45
-
-
@CEPB See [Where do I find the current C or C++ standard documents?](https://stackoverflow.com/q/81656/10147399). Final drafts are free and often good enough for educational use. You don't need to pay for the standard unless you're implementing a compiler or similar. – Aykhan Hagverdili Apr 25 '21 at 16:32
-
That's where my "confusion" lays, do `GCC` and `Clang` (GNU Project and LLVM) pay those fees. – Apr 25 '21 at 16:35
-
@CEPB they probably do. I don't have any inside knowledge on that. – Aykhan Hagverdili Apr 25 '21 at 18:19
0
If you need <string.h>
, include it yourself. You can also forward-declare memcpy
and use it without including anything:
void* memcpy( void *restrict dest, const void *restrict src, size_t count );

Aykhan Hagverdili
- 28,141
- 6
- 41
- 93
-
-
@CEPB then, they should go with forward declaration or should include `string.h` as any non-trivial project is going to include that anyway. – Aykhan Hagverdili Apr 24 '21 at 19:02
-
1Your answer is fine for me. I gave you up. I know the best way is to include it. I just did not want you go get penalized. – Apr 24 '21 at 22:34
-
1@CEPB thank you for the feedback. I removed that part of the answer. – Aykhan Hagverdili Apr 25 '21 at 06:50