3

Ac 、 Bc 、 Cc という 3 つのファイルがあり、そのすべてに #include common.h が含まれています。

common.h に「sys/socket.h」をインクルードし、common.h をマクロで保護します。

#ifndef __COMMON_H
#define __COMMON_H
// body of file goes here
#endif

コードをコンパイルすると、以下のようないくつかのエラーが発生します

In file included from /usr/include/sys/socket.h:40,
             from tcpperf.h:4,
             from wrapunix.c:1:
/usr/include/bits/socket.h:425: error: conflicting types for 'recvmmsg'
/usr/include/bits/socket.h:425: note: previous declaration of 'recvmmsg' was here
In file included from /usr/include/sys/socket.h:40,
             from tcpperf.h:4,
             from wrapsock.c:1:

wrapunix.c と wrapsock.c を見るとわかるように、どちらにも tcpperf.h が含まれていますが、tcpperf.h はマクロで保護されていますが、gcc は recvmsg が複数回宣言されていると不平を言っています。この問題を解決するにはどうすればよいですか?

更新: これは、問題を引き起こしている tcpperf.h のヘッダーです。

#ifndef _TCPPERF_H
#define _TCPPERF_H
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <regex.h>
#include <errno.h>
#include <sched.h>
#include <pthread.h>
#include <argp.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <linux/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <sys/wait.h>
#endif

上記のエラーは、次のような「-combine -fwhole-program」フラグを gcc に指定することで再現できます。

gcc -std=gnu99 -Wall -combine -fwhole-program -I. error.c wrapunix.c wrapsock.c file1.c file2.c -o file2 -lrt

4

2 に答える 2

0

エラーは、定義が重複しているだけではなく、「'recvmmsg' のタイプが競合しています」(等しい場合は許容されます) です。これは、.c ソースが 2 つの異なるバージョンの recvmmsg を受け取ることを意味します。1 つは直接 tcpperf.h を含めることによって、もう 1 つは sys/socket.h を介して含めることによって受け取ります。別の (おそらく古いバージョンの) recvmmsg を使用して、別のバージョンの tcpperf.h がインクルージョン パスの他の場所にあると思います。

于 2012-12-23T02:54:07.200 に答える
0

この問題はほぼ確実に に関連してい-combineます。これはちょっとした推測ですが、 の定義を見るとrecvmmsg:

extern int recvmmsg (int __fd, struct mmsghdr *__vmessages,
                     unsigned int __vlen, int __flags,
                     __const struct timespec *__tmo);

struct mmsghdr引数としてa を取ることに注意してください。ただし、このプロトタイプは無条件ですが、が設定struct mmsghdrされている場合にのみ定義されます。__USE_GNU

#ifdef __USE_GNU
/* For `recvmmsg'.  */
struct mmsghdr
  {
    struct msghdr msg_hdr;      /* Actual message header.  */
    unsigned int msg_len;       /* Number of received bytes for the entry.  */
  };
#endif

-combine基本的に、すべてのファイルを連結してからコンパイルするのと同じです。wrapunix.cのテキストとwrapsock.cそのテキストの間GNU_SOURCEに定義されている可能性はありますか? その場合、 の最初の定義でrecvmmsgstruct mmsghdrプロトタイプのみにローカルな の定義が使用され、2 番目の定義では実際の構造体が使用されます。これら 2 つの定義は互換性がなくなり、エラー メッセージが表示されます。

于 2013-03-20T06:45:34.537 に答える