Sunday, October 9, 2022

[SOLVED] Why file sys/types.h doesn't contain function definition of major(dev_t dev)?

Issue

An error is being reported when I use the function major(st_dev) in my code: "‘major’ was not declared in this scope"

Looking up rel="nofollow noreferrer">documentation in the man page, it suggests that the file sys/types.h contains the definition major(dev_t dev). When I check the file /usr/include/sys/types.h on Linux version 5.10.76-linuxkit, it doesn't contains definition of major(dev_t dev).

I believe the missing definition of major(dev_t dev) in the file /usr/include/sys/types.h is the source of the error.

So my question is why doesn't /usr/include/sys/types.h contains the major(dev_t dev) definition as documented in the manual page 3 entry?

major(3) - Linux man page
Name
makedev, major, minor - manage a device number
Synopsis
#define BSD SOURCE               /* See feature test macros(7) */
#include <sys/types.h>
dev_t makedev(int maj, int min);
unsigned int major(dev_t dev);
unsigned int minor(dev_t dev);
Description

misc

The code is below:

#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>



major(statbuf.st_dev);

Solution

The BSD include file <sys/types.h> originally created in 1982.

On Linux, the helper include <sys/types.h> also includes the <features.h> file. The provided function of this file has changed over time on Linux. That documentation is old, as it refers to BSD_SOURCE. In current versions, BSD_SOURCE is an alias for _DEFAULT_SOURCE.

To obtain those macros, you must include the <sys/sysmacros.h> file.



Answered By - James Risner
Answer Checked By - David Goodson (WPSolving Volunteer)