I've been reading about the linux GPIO functions and tried to make a small example using the function gpio_is_valid, but the compile fails because it can't find linux/gpio.h.

#include <iostream>

#include <linux/gpio.h>

int main()
    const bool is17Valid = !!gpio_is_valid(17);
    if (is17Valid)
        std::cout << "17 is valid!" << std::endl;
        std::cout << "17 is *not* valid!" << std::endl;

    return 0;

Is linux/gpio.h available on the Raspberry Pi? If not, are none of the functions described in the linux documentation available?

I'm using the original Debian "Squeeze" image.

  • 2
    Which distro are you using? – Alex Chamberlain Jul 18 '12 at 21:18
  • Did you setup up the linker?What are the error messages? Would "return 0;" be pointless since it isn't C? – ArchHaskeller Jul 18 '12 at 21:31
  • I'm using the original Debian "Squeeze" image and the error message was, can't find "linux/gpio.h". – Mark Ingram Jul 18 '12 at 21:34
  • You have to interact with the GPIO as root. Are you root? – Jivings Jul 18 '12 at 22:54
  • @Haskeller: C++ is backwards compatible with C, so you still need return 0; because you're in a function that returns an int. Omitting this results in a compiler error. – Malvineous Jul 18 '12 at 23:07

I don't think you need linux/gpio.h - as you stated in a comment, the RPi repo has a pretty redundant version of it.

The GPIO's are memory-mapped, which you can read about it in How does memory-mapped I/O addressing work?.

| improve this answer | |

Assuming you're compiling on the Pi itself, you'll probably need to install some development packages, as precompiled binaries don't need header files present to run (so they're not installed by default.)

I think in this case the name of the package you want is probably linux-headers, which will put gpio.h into /usr/include/linux/gpio.h. Because the file sits in /usr/include it means you don't need to specify this part of the path either in your code or to the compiler, so #include <linux/gpio.h> as you have there is enough, once you've installed the correct package.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.