I am converting some C code for a Raspberry Pi 3B to C++. This portion of the C code,
// Open /dev/mem
if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("Failed to open /dev/mem, try checking permissions.\n");
return -1;
}
p->map = mmap(
NULL,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
p->mem_fd, // File descriptor to physical memory virtual file '/dev/mem'
p->addr_p // Address in physical map that we want this memory block to expose
);
presents a challenge. The immediate issue is with the file descriptor returned by the C open function. C++ uses fstream, which will work for opening the file but when I get to the C function mmap I do not have a file descriptor.
mmap maps files into memory.
This is a learning experiment and I would like to stick to C++.
It does bring to mind a question. The RPi OS, Raspbian, has built-in all these C functions. Does it also have built-in the C++ equivalents or is it expected that one use the C functions and perhaps an extern "C"
statement?