0

Probably a really simple problem but I don't get it.

I'm trying to print the value of TUNSETIFF from linux/if_tun.h

#include <linux/if_tun.h>
#include <stdio.h>

int main()
{
    printf("%d", TUNSETIFF);
    return 0;
}

When I try to compile this code I get an error:

In file included from test.c:1:0:
test.c: In function `main`:
test.c:7:24: error: expected expression before `int`
    printf("%d", TUNSETIFF);
                 ^

I don't understand what is wrong with my code, isn't that how you print an integer? If I just do printf("Hello") it works just fine..

FGreg
  • 14,110
  • 10
  • 68
  • 110
  • How is `TUNSETIFF` defined? Is it a `#define` or a `const`? If it's `const` did you add `const`? (See https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum ) – Dai Feb 12 '18 at 02:47
  • From https://github.com/torvalds/linux/blob/master/include/uapi/linux/if_tun.h#L34 it is set as `#define TUNSETIFF _IOW('T', 202, int) ` – FGreg Feb 12 '18 at 02:48
  • What is the final resolution of `TUNSETIFF` though? Follow the rabbit-hole and see what `_IOW` is define as :) – Dai Feb 12 '18 at 02:48
  • It looks like the expansion of `TUNSETIFF` is `_IOW('T', 202, int)`. `int` is a type, not a variable. Is this a bug in the header? – Grant Sanders Feb 12 '18 at 02:57
  • @GrantSanders `_IOW` is a function used by `ioctl` to generate some platform dependent constants... I don't totally understand it but more info is here https://stackoverflow.com/questions/14626867/what-does-io-mean-in-c-headers-in-linux – FGreg Feb 12 '18 at 02:59

1 Answers1

2

You need to #include <sys/ioctl.h>. Otherwise the definition of TUNSETIFF is not expanded fully into an integer.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436