-2

I am using ssize_t in a piece of C code. I don't know which header file it is declared. So I start googling and then get buried and lost among many unrelated stuff.

This scenario happens over and over again to me, and it wastes much time. So, is there a conventional, probably efficient solution where you professional C coders find the right header file, or maybe you just remember them in mind? Thanks.

zell
  • 9,830
  • 10
  • 62
  • 115
  • 1
    Well there's no standard type called `ssize_type`, so I imagine you'll find it together with the rest of the library that wanted you to use that type. If you know _why_ you are using that type, it should explain itself where to find its declaration. – Lundin Apr 23 '18 at 11:55
  • Thanks. Typo corrected. – zell Apr 23 '18 at 11:59
  • No better, since `ssize_t` isn't standard either. The standard type `size_t` is found in stddef.h (which in turn is included by many other headers such as stdlib.h). – Lundin Apr 23 '18 at 12:31
  • @Lundin. Thank you. I did not know that ssize_t is non- standard. Maybe that was the reason I did not find its header! – zell Apr 23 '18 at 13:18

1 Answers1

2

Types don't require a corresponding C file, so that's a bit difficult.

If you know a function that uses the type, you can often read it's manual page, and see which headers are needed. For example you might know that ssize_t is returned by read(2), and then the friendly manual page: says:

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);

So clearly including <unistd.h> must provide a definition of ssize_t in addition to the function.

Another approach is grep:ing the system include directories, that's brute force but often helpful.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • maybe you can show the command of how you grep ? BTW, what is the stardard C include directory on a Mac OS? – zell Apr 23 '18 at 11:51
  • 1
    @zell Unfortunately there is no standard directory on the Mac. On mine, I have created a symlink `/usr/include` (which is the old Unix standard, and one I can remember), which points at the actual directory for the current version of clang, which I somehow discovered is `/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include`. – Steve Summit Apr 23 '18 at 12:00
  • @MartinJames There could be a thousand ways to grep. SO is such a great place because there are people here who are willing to _share_, and there are people here who are willing to _learn_. I am unsure what you can do here. – zell Apr 23 '18 at 12:36