我如何理解这个编译器错误:“...的多重定义”
I'm working on my exam assignment. It's due in almost 6 hours. Suddenly my program won't compile anymore with this error message:
我正在完成考试任务。它将在近6个小时内到期。突然,我的程序将不再编译此错误消息:
gcc -g -D DEBUG -c -o obj/stringops.o src/stringops.c gcc -g -D DEBUG -c -o obj/arrayops.o src/arrayops.c gcc -g -D DEBUG -c -o obj/fileops.o src/fileops.c gcc -g -D DEBUG -c -o obj/builtins.o src/builtins/*.c gcc -g -D DEBUG -c -o obj/tomashell.o src/tomashell.c gcc -g -D DEBUG -o bin/tomashell \ obj/stringops.o obj/arrayops.o obj/fileops.o obj/builtins.o \ obj/tomashell.o obj/tomashell.o: In function `n_processes': /root/sc/tomashell/src/safefork.c:11: multiple definition of `h_meta' obj/builtins.o:/root/sc/tomashell/src/builtins/history.c:4: first defined here obj/tomashell.o: In function `n_processes': /root/sc/tomashell/src/safefork.c:11: multiple definition of `h_meta_len' obj/builtins.o:/root/sc/tomashell/src/builtins/history.c:4: first defined here collect2: ld returned 1 exit status make: *** [bin/tomashell] Error 1
In this file:
在这个文件中:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/errno.h>
extern int errno;
#define MAX_PROCESSES 6
static int n_processes(void)
{ // <- THIS IS LINE 11
return system("exit `ps | wc -l`")/256;
}
pid_t safefork(void)
{
static int n_initial = -1;
if (n_initial == -1)
n_initial = n_processes();
else if (n_processes() >= n_initial+MAX_PROCESSES) {
sleep(2);
errno = EAGAIN; return (pid_t)-1;
}
return fork();
}
Someone please help me or kill me. I don't want to live in a world where this sort of error is possible.
有人请帮助我或杀了我。我不想生活在一个可能出现这种错误的世界。
Any ideas to what might be wrong?
什么想法可能是错的?
builtins/history.c
builtins/history.h
5 个解决方案
#1
2
Like the others have said, you should not "define" (either implicitly or explicitly allocate space for) a variable in your headers.
像其他人所说的那样,你不应该在标题中“定义”(隐式或显式地为空间分配空间)变量。
This example might help:
此示例可能有所帮助:
#ifndef HISTORY
#define HISTORY
...
/* Bad! Don't actually DEFINE (allocate space for) variables in a header!
* h_metablock* h_meta = NULL;
* int h_meta_len = 0;
*/
/* Better: declare "extern", then define in exactly ONE module (e.g. "main.c") */
extern h_metablock* h_meta;
extern int h_meta_len;
...
The same, of course, goes for any globals you might define in your .c/.cpp files.
当然,同样适用于您在.c / .cpp文件中定义的任何全局变量。
A global can only be "defined" exactly once.
全局只能被“定义”一次。
更多相关文章
- 铯:使用自己的OpenStreetMap服务器。:“未能获得图像块”错误
- OCaml编译错误:/ usr / bin / ld:找不到-lstr
- Linux 2.6.x 内核模块加载错误 “Invalid module format” 解决
- 在用apt源安装mysql-server时出现如下错误,需要安装mysql-server-
- 存储过程放在sql脚本里,命令行导入总是提示错误
- 解决缺少sql头文件编译错误
- “已有打开的与此命令相关联的 DataReader,必须首先将它关闭 ”错
- 查询自定义VO对象的sql
- Mysql5.7.10版本安装后空密码登录,退出后提示密码错误连接不上解