user15194465
user15194465

Reputation: 5

Is there a way to check if <string.h> is included?

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?

Upvotes: 0

Views: 616

Answers (3)

cugz
cugz

Reputation: 1

you can check for the include guards of string.h for instance:

#ifndef _STRING_H
#define foo "no\n"
#else
#define foo "yes\n"
#endif

successfully detects string.h on my laptop.

tried this based on an answer to a different question i saw:

Check whether function is declared with C preprocessor?

Upvotes: 0

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

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 );

Upvotes: 0

Andrew Henle
Andrew Henle

Reputation: 1

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 is size_t and the macro is NULL ...

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.

Upvotes: 2

Related Questions