0

There are two libraries zconf.h and unistd.h which are used to at least to get pid of the process. I generally test my code on Mac OSX and Ubuntu 18.04 in which they use zconf.h preferably(compiler offers zconf.h in lieu of unistd.h) if I forget to add, then if the code works, it's ok. However, in some prior day I needed to test the code in another machine AFAIR it has Ubuntu 10 or 12. Its compiler complained that there is no zconf.h. I wonder whether there is a way to check a machine has zconf.h, if not, use unistd.h. Can it be done using preprocessors like,

#ifdef ITS_IF_CONDITION
    #include <zconf.h>
#else
    #include <unistd.h>
7
  • The only zconf.h I know is part of zlib. If you want to use getpid(), you should just #include <unistd.h>, which will work on all unix systems. Commented Jul 6, 2018 at 8:20
  • If you find that an existing answer solved your problem, you can accept it. If it's missing just a small bit, you can add it as a comment or just edit the answer. If you came up with a solution yourself, you can post it as a new answer. But please don't edit answers into your question. Commented Jul 6, 2018 at 8:48
  • defined takes an identifier. After defined(__has_include the only possible valid token is ), not (<zconf. Commented Jul 6, 2018 at 8:49
  • Again, I don't know what zconf.h you're talking about. If you're talking about zlib, that's for compression, not PIDs. Commented Jul 6, 2018 at 8:55
  • 2
    Dude, it says in line 1 "zconf.h -- configuration of the zlib compression library". That's an internal header used by zlib. You're not supposed to use it in your own code. Also, it's generated dynamically by the configure script when you build/install zlib (this is what cyco130 was referring to by "autoconf"). Commented Jul 6, 2018 at 9:14

1 Answer 1

7

Newer versions of GCC, clang and MSVC compilers implement the __has_include feature. Although it's a C++ 17 feature, I believe all three support it in plain C too.

But the traditional (and probably more portable) way is to check the existence of include files in a config script before the build process. Both autoconf and cmake have ways to achieve this.

#ifdef __has_include
    #if __has_include(<zconf.h>)
        #include <zconf.h>
    #else
        #include <unistd.h>
    #endif
#else
    #include <unistd.h>
#endif
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.