首页 文章

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

提问于
浏览
1

我正在尝试为我的Raspberry Pi 2制作一个简单的TCP服务器程序 . 我已经制作了初始服务器代码并且它可以正常工作,但后来,我试图专门为服务器实现一个线程而我无法编译自那时候起 .

#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:错误:#错误此文件需要编译器和库支持即将推出的ISO C标准C 0x . 此支持目前是实验性的,必须使用-std = c 0x或-std = gnu 0x编译器选项启用 . main.cpp:在函数'int main()'中:main.cpp:79:2:错误:'thread'不是'std'main.cpp的成员:79:14:error:expected';'在'serverTask'main.cpp之前:84:13:错误:在'serverTask'中请求成员'join',这是非类型'int(int)'编译失败 .

那么我尝试使用 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:在成员函数'void std :: _ Bind_result <_Result中, _Functor(_Bound_args ...)> :: __ call(std :: tuple <_Args ...> &&,std :: _ Index_tuple <_Indexes ...>,typename std :: _ Bind_result <_Result,_Functor(_Bound_args ...) > :: __ enable_if_void <_Res> :: type)[with Res = void, Args = {},int ..._ Indexes = {},_ Result = void,_Functor = int,_Bound_args = {},typename std :: _ Bind_result < _Result,_Functor(_Bound_args ...)> :: __ enable_if_void <_Res> :: type = int]':/ usr / include / c /4.6/functional:1378:24:从'std :: _ Bind_result <_Result,_Functor实例化(_Bound_args ...)> :: result_type std :: _ Bind_result <_Result,_Functor(Bound_args ...)> :: operator()( Args && ...)[with Args = {}, Result = void,_Functor = int ,_Bound_args = {},std :: _ Bind_result <_Result,_Functor(_Bound_args ...)> :: result_type = void]'/ usr / include / c /4.6/thread:117:13:从'void std ::实例化线程:: _默认地将Impl <_Callable> :: _ M_run( )[with _Callable = std :: _ Bind_result]'main.cpp:86:1:从这里实例化/ usr / include / c /4.6/functional:1287:4:错误:'((std :: _ Bind_result *)this) - > std :: _ Bind_result :: _ M_f'不能用作函数编译失败 .

我的最后一招是安装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:在'struct std :: _ Bind_simple'的实例化中:/ / usr / include / c /4.9/thread:140:47:从'std :: thread :: thread(Callable &&, Args && ...)[需要_Callable = int; _Args = {}]'main.cpp:79:29:从这里需要/ usr / include / c /4.9/functional:1665:61:错误:'class std :: result_of'typedef typename中没有名为'type'的类型result_of <Callable( Args ...)> :: type result_type; ^ / usr / include / c /4.9/functional:1695:9:错误:'class std :: result_of'中没有名为'type'的类型_M_invoke(_Index_tuple <_Indices ...>)^编译失败 .

现在我很困惑,我不知道我做错了什么 .

更新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“test”“test.cpp”(在目录中:/ home / pi / TempServer)在/ usr / include / c /4.9/thread:39中包含的文件中:0,来自test.cpp:2:/ usr / include / c /4.9/functional:在'struct std :: _ Bind_simple'实例化时:/ usr / include / c /4.9/thread:140:47:需要' std :: thread :: thread(Callable &&, Args && ...)[with _Callable = int; _Args = {}]'test.cpp:12:29:从这里需要/ usr / include / c /4.9/functional:1665:61:错误:'class std :: result_of'typedef typename中没有名为'type'的类型result_of <Callable( Args ...)> :: type result_type; ^ / usr / include / c /4.9/functional:1695:9:错误:'class std :: result_of'中没有名为'type'的类型_M_invoke(_Index_tuple <_Indices ...>)^编译失败 .

1 回答

  • 2

    这一行:

    std::thread serverTask(7000);
    

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

    你可能意味着:

    std::thread foo(serverTask, 7000);
    

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

相关问题