c - 拆分 ac 文件并使用 Makefile
问题描述
我的目标是将我的程序分成几个文件并使用 Makefile 来运行它。但是,我不断收到此错误:我曾经在我的主文件中初始化“int top”,但我认为这可能是问题所在,但仍然没有结果,我似乎无法在网上找到任何可以帮助我的东西这个错误,如果有人知道问题或有用的资源,他们可以推荐给我,那就太好了:)
gcc -o hw5 hw5.o pushnpop.o
pushnpop.o:(.bss+0x0): multiple definition of `top'
hw5.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'hw5' failed
make: *** [hw5] Error 1
********这是我的代码**************** *******hw5.c*********
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
#include "pushnpop.h"
#define MAX 100
int emptyCheck(char *stack)
{
if(stack[0] == '\0')
return 1;
else
return 0;
}
int main()
{
/*int top = 0;*/
char c;
char line[10000];
FILE *file;
char stack[MAX];
char fileName;
printf("Please enter a file name with a .c extension\n");
scanf("%s", &fileName);
/*printf("testing the name:%s\n", &fileName);*/
file = fopen(&fileName, "r");
/*error checking*/
if (file == NULL)
{
printf("Cannot access file\n");
exit(0);
}
for(int i = 0; i < MAX; i++ )
{
stack[i] = '\0';
}
/*getting the data from the file*/
int lineCounter = 1;
while(fgets(line, 10000, file) != NULL)
{
int j = 0;
while(line[j] != '\n')
{
if(line[j] == '{')
{
push(stack);
}
else if(line[j] == '}')
{
if(emptyCheck(stack) == 1)
{
printf("too many } on line %d\n", lineCounter);
break;
}
else
pop(stack);
}
j++;
}
if(emptyCheck(stack)==0)
{
printf("too many { on line %d\n", lineCounter);
}
if(fgets(line, 10000, file) == NULL)
{
printf("END of FILE\n");
exit(0);
}
for(int i = 0; i<MAX; i++)
stack[i] = '\0';
lineCounter++;
top = 0;
}
fclose(file);
return 0;
}
****我的第二个文件***** *****pushnpop.c******
#include "pushnpop.h"
char pop(char *stack)
{
char *temp;
temp = stack;
top--;
temp[top] = '\0';
return *temp;
}
char push(char *stack)
{
char *temp;
temp = stack;
temp[top] = '{';
top++;
return *temp;
}
******我的第三个文件** ****pushnpop.h********
#ifndef pnp
#define pnp
int top = 0;
char pop(char *stack);
char push(char *stack);
#endif
********制作文件********
all: hw5
hw5: hw5.o pushnpop.o
gcc -o hw5 hw5.o pushnpop.o
pushnpop.o: pushnpop.c
gcc -o pushnpop.o -c pushnpop.c -W -Wall -pedantic
hw5.o: hw5.c pushnpop.h
gcc -o hw5.o -c hw5.c -W -Wall -pedantic
clean:
rm -rf *.o
mrproper: clean
rm -rf pushnpop
解决方案
第 1 步,将 移至int top
和pushnpop
不top
从访问main
。喜欢,
// pushnpop.c
#include "pushnpop.h"
int top = 0;
char pop(char *stack)
{
然后修改pushnpop.h
以声明函数extern
。喜欢,
// pushnpop.h
#ifndef pnp
#define pnp
extern char pop(char *stack);
extern char push(char *stack);
#endif
别忘了注释掉
// top = 0;
在main
.
推荐阅读
- r - 插入新行时,反应值重置为 NA rhandsontable 闪亮
- bash - 如何在没有 Brew 的情况下重新创建 Brew 别名?
- ubuntu - 在 VR 和 Ubuntu 服务器上使用 Bullet3 Pyhsics Simulator
- c++ - 防止重载方法上的 PLT 断点
- xcode - 如何更改 xcode 项目的 firebase 数据库?
- javascript - 在一个查询中为每篇文章选择文章 + 多条评论
- scala - spark 无法序列化 lambda
- babeljs - 使用 babel 像 iife 一样汇总输出
- kubernetes - Prometheus 操作员警告“资源版本太旧”
- c# - 自定义通知窗口窗体而不更改当前工作窗口的焦点