maz*_*zix 43 c linker linker-errors
我定义了一个特殊文件: config.h
我的项目还有文件:
t.c, t.h
pp.c, pp.h
b.c b.h
l.cpp
Run Code Online (Sandbox Code Playgroud)
和#includes:
在tc:
#include "t.h"
#include "b.h"
#include "pp.h"
#include "config.h"
Run Code Online (Sandbox Code Playgroud)
在bc:
#include "b.h"
#include "pp.h"
Run Code Online (Sandbox Code Playgroud)
在pp.c:
#include "pp.h"
#include "config.h"
Run Code Online (Sandbox Code Playgroud)
在l.cpp中:
#include "pp.h"
#include "t.h"
#include "config.h"
Run Code Online (Sandbox Code Playgroud)
我的*.h文件中没有include指令,只在*.c文件中.我在config.h中定义了这个:
const char *names[i] =
{
"brian", "stefan", "steve"
};
Run Code Online (Sandbox Code Playgroud)
并在l.cpp,tc,pp.c中需要该数组但是我收到此错误:
pp.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
t.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [link] Error 1
Run Code Online (Sandbox Code Playgroud)
我*.h在我的项目中使用的每个文件中都包含了警卫.任何帮助解决这个问题
Yu *_*Hao 102
不要在标题中定义变量.将声明放在其中一个.c文件的标头和定义中.
在config.h中
extern const char *names[];
Run Code Online (Sandbox Code Playgroud)
在某些.c文件中:
const char *names[] =
{
"brian", "stefan", "steve"
};
Run Code Online (Sandbox Code Playgroud)
如果在头文件中放置全局变量的定义,那么此定义将转到包含此头的每个.c文件,并且您将获得多个定义错误,因为varible可能被多次声明但是只能定义一次.
小智 26
公共函数的声明放在头文件中,是的,但定义在标题中也绝对有效!如果要在实用程序函数的标题中定义事物,而不想在每个c文件中再次定义,则可以将定义声明为静态(整个程序只允许1个副本).IE定义枚举和静态函数以将枚举转换为字符串.然后,您不必为包含标头的每个.c文件重写enum到字符串转换器.:)