Reputation: 11
I'm trying to use the fcntl() system call to create locks on a file, but in order to do that you need to pass in an instance of the flock struct, which is a structure that is defined in the fcntl.h file. I've watched Youtube videos, scoured the internet for solutions, but still, I cannot figure out how to get my code to compile without error. I create an instance of flock struct as follows:
#include <fcntl.h>
int start = calculate_buffer(i);
int lock_size = calculate_lock_size(i, j);
int pid = getpid();
struct flock fl{start, lock_size, pid, F_UNLCK, SEEK_SET};
If you are thinking the problem is the order in which I am passing the arguments, that is not the problem, as this is how they are ordered for macOS (why its different from Linux I don't know). Anyways, I've tried compiling my code using:
g++ main.cpp
gcc main.cpp
Both of which produce the following error:
main.cpp:111:20: error: expected ';' at end of declaration
struct flock fl{start, lock_size, pid, F_UNLCK, SEEK_SET};
^
;
1 warning and 1 error generated.
Why is this happening? And what can I do to fix it? Here are some references I've used that show that this is exactly how its meant to be done:
https://pubs.opengroup.org/onlinepubs/009695399/functions/fcntl.html https://www.youtube.com/watch?v=whYnqxKSBBo&t=339s
Upvotes: 0
Views: 587
Reputation: 1924
Initializer lists need an equals between the variable to initialize and the list itself. Your declaration should read:
struct flock fl = {start, lock_size, pid, F_UNLCK, SEEK_SET};
Upvotes: 2