错误:重新定义'struct timespec'

时间:2015-09-23 11:56:39

标签: c networking struct

myheader.h

#ifndef _MYHEAD_
#define _MYHEAD_

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <time.h>

#endif

echoClient.c

#include "myheader.h"

int clock_gettime(clockid_t clock_id, struct timespec *tp);
int nanosleep(const struct timespec *req, struct timespec *rem);

typedef struct timespec // error message line
{
    time_t tv_sec;
    long tv_nsec;
};

int main(int argc, char* argv[])
{
    int s;
    char* servName;
    int servPort;
    char* string;
    char buf[256 + 1];
    int len = 0;
    int maxLen = sizeof(buf);
    struct sockaddr_in serverAddr;
    timespec t1,t2,t3;
    double practice;
    int temp = 0;
    int n;



    if(argc != 4)
    {
            printf("Usage: client <server> <port> <virtual packet size> <string>\n");
            exit(1);
    }

    servName = argv[1];
    servPort = atoi(argv[2]);

    string = argv[3];

    memset(&buf, 0, sizeof(buf));

    /* Create remote (server) socket address */
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    inet_pton(AF_INET, servName, &serverAddr.sin_addr);     // server IP addr
    serverAddr.sin_port = htons(servPort);

    practice = clock_gettime(CLOCK_REALTIME,&t3);
    printf("time %f\n",practice);

    /* Creat socket */
    if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
            perror("Error: socket creation failed\n");
            exit(1);
    }

    // send the echo message
    n = sendto(s, string, strlen(string), 0,
            (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    printf("-- actual tx frame size:  (%d bytes)\n",n);

    /* here, the message has been sent, and the echo will come */

    /* receive Echo */
    memset(&buf, 0, sizeof(buf));
    len = recvfrom(s, buf, sizeof(buf), 0, NULL, NULL);
    buf[len] = '\0';

    printf("Echoed string received: ");
    printf("%s\n", buf);

    close(s);
    exit(0);
}

如您所见,

  

“错误:重新定义'struct timespec'

当我尝试编译时发生了

  

/usr/include/time.h:122:错误:'struct timespec'的先前定义

也发生了。我该如何修改这段代码。

请帮帮我。

3 个答案:

答案 0 :(得分:2)

time.h按以下方式定义为struct timespec { __time_t tv_sec; /* Seconds. */ __syscall_slong_t tv_nsec; /* Nanoseconds. */ };

int clock_gettime(clockid_t clock_id, struct timespec *tp);
int nanosleep(const struct timespec *req, struct timespec *rem);

删除代码中的结构定义。

必须删除代码顶部的原型,

timespec t1,t2,t3;

已在time.h中定义

最后一件事,定义 struct timespec t1,t2,t3; 一定是 -Wall

修改

如果添加temp选项,编译器会向您发出有关未使用变量的警告:maxlent1t2requests_session = requests.session() requests_session.mount('file://', LocalFileAdapter()) ra=requests_session.get('file://X:\somefile.htm') print ra.content

答案 1 :(得分:1)

struct timespectime.h中定义。不要再定义自己的了。

您应该删除typedef struct timespec { ... };中的echoClient.c部分。

答案 2 :(得分:0)

我在Ecliepse Neon IDE中遇到了同样的错误,我通过添加&#39; -DHAVE_STRUCT_TIMESPEC&#39;来解决它。在C / C ++ Build中 - &gt;设置 - &gt; GCC C ++编译器 - &gt;其他 - &gt;其他国旗

https://github.com/ghostlander/nsgminer/issues/1