在Raspbian上使用C ++上的线程,无法编译

时间:2015-10-27 02:09:56

标签: c++ multithreading gcc raspberry-pi2

我试图为我的Raspberry Pi 2制作一个简单的TCP服务器程序。我已经制作了初始服务器代码并且它可以工作,但是后来,我尝试专门为服务器实现一个线程而且我没有&# 39;从那时起就能够编译。

#include <iostream>
#include <thread>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int serverTask(int port){
    int n;
    char buffer[256];
    /* Socket File Descriptors */
    int socketFd, socketFdNew;
    socklen_t clientLength;

    struct sockaddr_in serverAddress, clientAddress;

    /* TCP Socket */
    socketFd = socket(AF_INET, SOCK_STREAM, 0);
    if(socketFd < 0){
        error("ERROR opening socket\n");
    }

    /* Fills serverAddress with zeroes */
    bzero((char *) &serverAddress, sizeof(serverAddress));

    /* ServerAddress configuration */
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = INADDR_ANY;
    serverAddress.sin_port = htons(port);

    /* Biding socket to server address */
    if(bind(socketFd, (struct sockaddr *) &serverAddress,sizeof(serverAddress)) < 0){
        error("ERROR on binding");
    }

    /* Listen for incoming connections, 5 is the number of queued devices */
    listen(socketFd,5);

    /* accept freezes the running thread until a client is found */
    clientLength = sizeof(clientAddress);
    socketFdNew = accept(socketFd, (struct sockaddr *) &clientAddress, &clientLength);
    if (socketFd < 0){
        error("ERROR on accept");
    }

    /* Fill buffer with zeroes */
    bzero(buffer,256);

    /* Reads up to 255 characters in the socket */
    n = read(socketFdNew, buffer, 255);
    if (n < 0){
        error("ERROR reading from socket");
    }

    printf("Received: %s\n",buffer);

    /* Responds with placeholder value */
    n = write(socketFdNew,"40.12\n",7);
    if (n < 0){
        error("ERROR writing to socket");
    }
    close(socketFdNew);
    close(socketFd);
    return 0;
}

int main()
{
    std::thread serverTask(7000);

    std::cout <<"Hello World!\n";

    serverTask.join();
    return 0;
}

我的第一次尝试只是使用g++ -Wall -o "main" "main.cpp"

将代码构建为任何其他代码

这给我带来了以下错误:

  

在/usr/include/c++/4.6/thread:35:0中包含的文件中,                    来自main.cpp:2:/usr/include/c++/4.6/bits/c++0x_warning.h:32:2:错误:#error这   文件需要编译器和库支持即将推出的ISO C ++   标准,C ++ 0x。这种支持目前是实验性的,必须是   使用-std = c ++ 0x或-std = gnu ++ 0x编译器选项启用。   main.cpp:在函数&#39; int main()&#39;:main.cpp:79:2:错误:&#39; thread&#39;是   不是&#39; std&#39;的成员main.cpp:79:14:错误:预期&#39;;&#39;之前   &#39; serverTask&#39; main.cpp:84:13:错误:请求会员加入&#39;在   &#39; serverTask&#39;,属于非类型&#39; int(int)&#39;汇编   失败。

然后我尝试使用C++11,因此我使用g++ -Wall -std=c++0x -pthread -o "main" "main.cpp"进行了重新编译,并获得以下内容:

  

在/usr/include/c++/4.6/thread:39:0中包含的文件中,                    来自main.cpp:2:/usr/include/c++/4.6/functional:在成员函数&#39; void std :: _ Bind_result&lt; _Result,_Functor(_Bound_args   ...)&gt; :: __ call(std :: tuple&lt; _Args ...&gt;&amp;&amp;,std :: _ Index_tuple&lt; _Indexes   ...&gt;,typename std :: _ Bind_result&lt; _Result,_Functor(_Bound_args   ...)&gt; :: __ enable_if_void&lt; _Res&gt; :: type)[with _Res = void,_ Args = {},   int ..._ Indexes = {},_ Result = void,_Functor = int,_Bound_args =   {},typename std :: _ Bind_result&lt; _Result,_Functor(_Bound_args   ...)&gt; :: __ enable_if_void&lt; _Res&gt; :: type = int]&#39;:   /usr/include/c++/4.6/functional:1378:24:实例化   &#39; std :: _ Bind_result&lt; _Result,_Functor(_Bound_args ...)&gt; :: result_type   std :: _ Bind_result&lt; _Result,_Functor(_Bound_args   ...)&gt; :: operator()(_ Args&amp;&amp; ...)[with _Args = {},_ Result = void,   _Functor = int,_Bound_args = {},std :: _ Bind_result&lt; _Result,_Functor(_Bound_args ...)&gt; :: result_type = void]&#39; /usr/include/c++/4.6/thread:117:13:从&#39; void实例化   std :: thread :: _ Impl&lt; _Callable&gt; :: _ M_run()[with _Callable =   的std :: _ Bind_result]&#39; main.cpp:86:1:实例化   这里/usr/include/c++/4.6/functional:1287:4:错误:   &#39;((STD :: _ Bind_result *)此) - &GT;的std :: _ Bind_result :: _ M_f&#39;不能用作函数编译失败。

我的最后一招是安装g ++ 4.9。安装完成后,我尝试了g++-4.9 -Wall -std=gnu++0x -pthread -o "main" "main.cpp",得到了以下结果:

  

在/usr/include/c++/4.9/thread:39:0中包含的文件中,                    来自main.cpp:2:/usr/include/c++/4.9/functional:实例化&#39; struct std :: _ Bind_simple&#39;:   /usr/include/c++/4.9/thread:140:47:需要   &#39; std :: thread :: thread(_Callable&amp;&amp;,_ Args&amp;&amp; ...)[with _Callable = int;   _Args = {}]&#39; main.cpp:79:29:从这里要求/usr/include/c++/4.9/functional:1665:61:错误:没有命名的类型&#39; type&#39;   在&#39; class std :: result_of&#39;          typedef typename result_of&lt; _Callable(_ Args ...)&gt; :: type result_type;                                                                ^ /usr/include/c++/4.9/functional:1695:9:错误:没有名为&#39; type&#39;在   &#39; class std :: result_of&#39;            _M_invoke(_Index_tuple&LT; _Indices ...&GT)            ^编译失败。

现在我很困惑,我不知道自己做错了什么。

更新1:尝试使用更简单的代码

#include <iostream>
#include <thread>
#include <stdio.h>


int serverTask(int port){
    return 0;
}

int main()
{
    std::thread serverTask(7000);


    std::cout <<"Hello World!\n";

    serverTask.join();
    return 0;
}

结果保持不变:

  

g ++ - 4.9 -Wall -std = gnu ++ 0x -pthread -o&#34; test&#34; &#34; TEST.CPP&#34; (在   目录:/ home / pi / TempServer)包含在的文件中   /usr/include/c++/4.9/thread:39:0,                    来自test.cpp:2:/usr/include/c++/4.9/functional:实例化&#39; struct std :: _ Bind_simple&#39;:   /usr/include/c++/4.9/thread:140:47:需要   &#39; std :: thread :: thread(_Callable&amp;&amp;,_ Args&amp;&amp; ...)[with _Callable = int;   _Args = {}]&#39; test.cpp:12:29:从这里要求/usr/include/c++/4.9/functional:1665:61:错误:没有命名的类型&#39; type&#39;   在&#39; class std :: result_of&#39;          typedef typename result_of&lt; _Callable(_ Args ...)&gt; :: type result_type;                                                                ^ /usr/include/c++/4.9/functional:1695:9:错误:没有名为&#39; type&#39;在   &#39; class std :: result_of&#39;            _M_invoke(_Index_tuple&LT; _Indices ...&GT)            ^编译失败。

1 个答案:

答案 0 :(得分:3)

这一行:

std::thread serverTask(7000);

声明一个名为serverTask的变量,并尝试传递7000作为要在线程中运行的函数的名称。但是7000不是函数。不幸的是,g ++在这里给出了非常神秘的错误消息。

你可能意味着:

std::thread foo(serverTask, 7000);

使用函数调用serverTask(7000)启动一个线程。