PID没有使用fork()

时间:2017-07-21 15:33:37

标签: c++ gcc raspberry-pi

所以我有一个简单的代码应该在Raspberry Pi的后台运行omxplayer:

    #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;
}

但是当我尝试使用gcc play.cpp -o play编译它时,不希望编译此代码,它给了我这些错误:

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");

我一直没有使用Raspberry编译器或c编码所以我有点生锈我可能忘了一些愚蠢但我找不到什么...我可以帮助我谢谢你提前!

1 个答案:

答案 0 :(得分:1)

您是否尝试将其封装在main()方法中?

#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;
}