C中的重复符号错误 [英] duplicate symbol error in C

查看:94
本文介绍了C中的重复符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码布局如下

header.h

#ifndef _header_h
#define _header_h
void empty(float *array, int l)
{
    int i;
    for (i=1 ; i<=l ; i++)
    {
        array[i]=0;
    }

}

#endif

和两个文件(我们称它们为file1.c和file2.c)

and two files (lets call them file1.c and file2.c)

#include "header.h"

void function/*1 or 2*/(){

     ....
     empty(a,b);
     ....
}

因此,编译工作正常,但链接器命令失败,因为编译器说存在重复的函数定义.如何仍然使用头文件来避免这种情况?当我仅在标头中定义函数并创建另一个包含完整功能的.c文件时,它工作正常.我一直以为在标题中声明它是方法.

So compiling works fine but the linker command fails as the compiler says that there is a duplicate function definition. How can I avoid that with still using the header file? It is working fine when I only define the function in the header and create another .c file containing the full function. I always thought declaring it in the header is the way to go.

推荐答案

我一直以为在标题中声明它是方法.

I always thought declaring it in the header is the way to go.

是的.在标题中声明是可以的.不过,在标题中定义并没有什么好处. (除非它是static inline,但这些天您可能不想这样做.)

Yes, it is. Declaring it in the header is fine. It is not any good to define it in a header, though. (unless it's static inline, but you probably don't want to do that these days.)

这篇关于C中的重复符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆