【问题标题】:error: redefinition of 'struct timespec'错误:重新定义“struct timespec”
【发布时间】:2015-09-23 11:56:38
【问题描述】:

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' 的先前定义

也发生过。我该如何修改此代码。

请帮帮我。

【问题讨论】:

  • 编译器告诉你答案。
  • 显示准确的编译命令。

标签: c networking struct


【解决方案1】:

timespec按如下方式定义成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);

已经在time.h中定义了

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

编辑

如果添加-Wall 选项,编译器会针对未使用的变量发出警告:tempmaxlent1t2

【讨论】:

  • 首先感谢您的回复。根据您的建议,我尝试删除结构行。但是,我遇到了这样的编译错误。“echoClient.c:(.text+0xe5): undefined reference to `clock_gettime'”
【解决方案2】:

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

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

【讨论】:

  • 根据您的建议,我尝试删除结构行。但是,我遇到了这样的编译错误。“echoClient.c:(.text+0xe5): undefined reference to `clock_gettime'”
【解决方案3】:

我在 Ecliepse Neon IDE 中遇到了同样的错误,我通过在 C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous -> Others flag 中添加“-DHAVE_STRUCT_TIMESPEC”解决了它

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多