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.

全局只能被“定义”一次。

更多相关文章

  1. 铯:使用自己的OpenStreetMap服务器。:“未能获得图像块”错误
  2. OCaml编译错误:/ usr / bin / ld:找不到-lstr
  3. Linux 2.6.x 内核模块加载错误 “Invalid module format” 解决
  4. 在用apt源安装mysql-server时出现如下错误,需要安装mysql-server-
  5. 存储过程放在sql脚本里,命令行导入总是提示错误
  6. 解决缺少sql头文件编译错误
  7. “已有打开的与此命令相关联的 DataReader,必须首先将它关闭 ”错
  8. 查询自定义VO对象的sql
  9. Mysql5.7.10版本安装后空密码登录,退出后提示密码错误连接不上解

随机推荐

  1. Android 多线程之 AsyncTask
  2. Android(安卓)-- Vibrator
  3. android 是什么
  4. Android的常见错误及解决办法
  5. Unity 与 Android (Android Studio)的交
  6. android中android:wight详解
  7. 横屏和竖屏切换不用销毁重新建立android:
  8. android 9.0系统重启的代码详解
  9. Android内核源码阅读---ashmem.c
  10. Android的一些常用命令提示符(cmd)指令