Issue
So I have a simple code that is supposed to run omxplayer in background on my Raspberry Pi :
#include <stdlib.h>
#include<stdio.h>
int main()
{
int pid;
pid=fork();
if(pid==0)
{
//printf("I am the child\n");
execlp("/usr/bin/omxplayer", " ", "/home/pi/projects/my_project/aud$
_exit(0);
}
else
{
//printf("I am the parent\n");
wait();
}
system("killall omxplayer.bin");
return 0;
}
But this code doesn't want to be compiled when I try to compile it using gcc play.cpp -o play
it gives me these errors :
play.cpp:5:1: error: ‘pid’ does not name a type
pid=fork();
^
play.cpp:6:1: error: expected unqualified-id before ‘if’
if(pid==0)
^
play.cpp:12:1: error: expected unqualified-id before ‘else’
else
^
play.cpp:18:7: error: expected constructor, destructor, or type conversion before ‘(’ token
system("killall omxplayer.bin");
It's been along time I haven't been on a Raspberry compiler or on c coding so I'm a bit rusted and I may have forgot something stupid but I can't find what.. I fyou can help me thanks in advance !
Solution
Have you tried encapsulating it in a main() method?
#include <stdlib.h>
#include<stdio.h>
int main(int argc, char **argv)
{
int pid;
pid=fork();
if(pid==0)
{
//printf("I am the child\n");
execlp("/usr/bin/omxplayer", " ", "/home/pi/projects/my_project/audio/my_file.wav", NULL);
_exit(0);
}
else
{
//printf("I am the parent\n");
wait();
}
system("killall omxplayer.bin");
return 0;
}
Answered By - dddJewelsbbb