可以重复导致编译错误的C源代码吗?

时间:2011-08-21 07:15:29

标签: c linux-kernel compiler-errors syntax-error

正如另一个question I had asked earlier我想我可能已经找到了我的问题,但我需要专家的眼睛。

我发现文件/net/ipv4/tcp_zero_copy.c导致内核编译失败。

我对C或C ++的了解很少,当我在C / C ++编辑器中查看该文件时,看起来相同的代码重复过来(我认为是4次)。

我的问题很简单;

这是否足以导致编译器出现问题?如果相同的代码在同一个文件中反复出现?

这是文件的源代码(从头到尾总共148行);

/*
 *  Support routines for TCP zero copy transmit
 *
 *  Created by Vladislav Bolkhovitin
 *
 *  This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      version 2 as published by the Free Software Foundation.
 */

#include <linux/skbuff.h>

net_get_page_callback_t net_get_page_callback __read_mostly;
EXPORT_SYMBOL(net_get_page_callback);

net_put_page_callback_t net_put_page_callback __read_mostly;
EXPORT_SYMBOL(net_put_page_callback);

/*
 * Caller of this function must ensure that at the moment when it's called
 * there are no pages in the system with net_priv field set to non-zero
 * value. Hence, this function, as well as net_get_page() and net_put_page(),
 * don't need any protection.
 */
int net_set_get_put_page_callbacks(
    net_get_page_callback_t get_callback,
    net_put_page_callback_t put_callback)
{
    int res = 0;

    if ((net_get_page_callback != NULL) && (get_callback != NULL) &&
        (net_get_page_callback != get_callback)) {
        res = -EBUSY;
        goto out;
    }

    if ((net_put_page_callback != NULL) && (put_callback != NULL) &&
        (net_put_page_callback != put_callback)) {
        res = -EBUSY;
        goto out;
    }

    net_get_page_callback = get_callback;
    net_put_page_callback = put_callback;

out:
    return res;
}
EXPORT_SYMBOL(net_set_get_put_page_callbacks);
/*
 *  Support routines for TCP zero copy transmit
 *
 *  Created by Vladislav Bolkhovitin
 *
 *  This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      version 2 as published by the Free Software Foundation.
 */

#include <linux/skbuff.h>

net_get_page_callback_t net_get_page_callback __read_mostly;
EXPORT_SYMBOL(net_get_page_callback);

net_put_page_callback_t net_put_page_callback __read_mostly;
EXPORT_SYMBOL(net_put_page_callback);

/*
 * Caller of this function must ensure that at the moment when it's called
 * there are no pages in the system with net_priv field set to non-zero
 * value. Hence, this function, as well as net_get_page() and net_put_page(),
 * don't need any protection.
 */
int net_set_get_put_page_callbacks(
    net_get_page_callback_t get_callback,
    net_put_page_callback_t put_callback)
{
    int res = 0;

    if ((net_get_page_callback != NULL) && (get_callback != NULL) &&
        (net_get_page_callback != get_callback)) {
        res = -EBUSY;
        goto out;
    }

    if ((net_put_page_callback != NULL) && (put_callback != NULL) &&
        (net_put_page_callback != put_callback)) {
        res = -EBUSY;
        goto out;
    }

    net_get_page_callback = get_callback;
    net_put_page_callback = put_callback;

out:
    return res;
}
EXPORT_SYMBOL(net_set_get_put_page_callbacks);
/*
 *  Support routines for TCP zero copy transmit
 *
 *  Created by Vladislav Bolkhovitin
 *
 *  This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      version 2 as published by the Free Software Foundation.
 */

#include <linux/skbuff.h>

net_get_page_callback_t net_get_page_callback __read_mostly;
EXPORT_SYMBOL(net_get_page_callback);

net_put_page_callback_t net_put_page_callback __read_mostly;
EXPORT_SYMBOL(net_put_page_callback);

/*
 * Caller of this function must ensure that at the moment when it's called
 * there are no pages in the system with net_priv field set to non-zero
 * value. Hence, this function, as well as net_get_page() and net_put_page(),
 * don't need any protection.
 */
int net_set_get_put_page_callbacks(
    net_get_page_callback_t get_callback,
    net_put_page_callback_t put_callback)
{
    int res = 0;

    if ((net_get_page_callback != NULL) && (get_callback != NULL) &&
        (net_get_page_callback != get_callback)) {
        res = -EBUSY;
        goto out;
    }

    if ((net_put_page_callback != NULL) && (put_callback != NULL) &&
        (net_put_page_callback != put_callback)) {
        res = -EBUSY;
        goto out;
    }

    net_get_page_callback = get_callback;
    net_put_page_callback = put_callback;

out:
    return res;
}
EXPORT_SYMBOL(net_set_get_put_page_callbacks);

**编辑**

此外,我只是使用Notepad ++来比较它们看起来结束的各个代码块。根据比较工具,所有3个区块相互匹配。

这是编译器输出从死亡的地方返回给我的命令提示符。

  CC      net/ipv4/tcp_zero_copy.o
net/ipv4/tcp_zero_copy.c:63:1: error: redefinition of â__kcrctab_net_get_page_callbackâ
net/ipv4/tcp_zero_copy.c:14:1: note: previous definition of â__kcrctab_net_get_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:63:1: error: redefinition of â__kstrtab_net_get_page_callbackâ
net/ipv4/tcp_zero_copy.c:14:1: note: previous definition of â__kstrtab_net_get_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:63:1: error: redefinition of â__ksymtab_net_get_page_callbackâ
net/ipv4/tcp_zero_copy.c:14:1: note: previous definition of â__ksymtab_net_get_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:66:1: error: redefinition of â__kcrctab_net_put_page_callbackâ
net/ipv4/tcp_zero_copy.c:17:1: note: previous definition of â__kcrctab_net_put_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:66:1: error: redefinition of â__kstrtab_net_put_page_callbackâ
net/ipv4/tcp_zero_copy.c:17:1: note: previous definition of â__kstrtab_net_put_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:66:1: error: redefinition of â__ksymtab_net_put_page_callbackâ
net/ipv4/tcp_zero_copy.c:17:1: note: previous definition of â__ksymtab_net_put_page_callbackâ was here
net/ipv4/tcp_zero_copy.c:74:5: error: redefinition of ânet_set_get_put_page_callbacksâ
net/ipv4/tcp_zero_copy.c:25:5: note: previous definition of ânet_set_get_put_page_callbacksâ was here
net/ipv4/tcp_zero_copy.c:98:1: error: redefinition of â__kcrctab_net_set_get_put_page_callbacksâ
net/ipv4/tcp_zero_copy.c:49:1: note: previous definition of â__kcrctab_net_set_get_put_page_callbacksâ was here
net/ipv4/tcp_zero_copy.c:98:1: error: redefinition of â__kstrtab_net_set_get_put_page_callbacksâ
net/ipv4/tcp_zero_copy.c:49:1: note: previous definition of â__kstrtab_net_set_get_put_page_callbacksâ was here
net/ipv4/tcp_zero_copy.c:98:1: error: redefinition of â__ksymtab_net_set_get_put_page_callbacksâ
net/ipv4/tcp_zero_copy.c:49:1: note: previous definition of â__ksymtab_net_set_get_put_page_callbacksâ was here
make[2]: *** [net/ipv4/tcp_zero_copy.o] Error 1
make[1]: *** [net/ipv4] Error 2
make: *** [net] Error 2
root@dev01:/usr/src/linux# ls

2 个答案:

答案 0 :(得分:1)

这取决于重复的内容,

重复的函数声明不会出现任何错误 重复的函数定义会产生错误 使用相同名称创建或定义的变量会产生错误。

#2&amp; #3在破坏ODR(一个定义规则)时会出错。

void doSomething(); //no error
void doSomething();
void doSomething();

void doSomething()
{

}

int main()
{
    int i; //error
    int i;
    doSomething();
    return 1;
}

在此代码中:

net_get_page_callback_t net_get_page_callback __read_mostly;

定义一个变量并重复这样做,导致同一命名变量的多重定义,从而导致重定义错误。

答案 1 :(得分:1)

在翻译单元中对函数进行多重定义是错误的。这就是这里发生的事情。尝试从编译中发布前几条错误消息。