Makefile头目录错误

Makefile header directory errors

本文关键字:错误 Makefile      更新时间:2023-10-16

我正试图找出我们的错误,我一直得到与make。我有几个源文件位于assign1目录以及includes目录(包含bb.h)。我要做的是在创建gq.o文件时包含bb.h头文件。由于这个错误,除了gq.o之外,所有其他目标文件都可以正常编译:

z1755294@hopper:~/assign1/assign1$ make
g++ -c fr.cc
g++ -c vw.cc
g++ -c ac.cc
g++ -c pg.cc
g++ -c gq.cc -I./includes
g++ -o output fr.o gq.o vw.o ac.o pg.o
gq.o: In function `main':
gq.cc:(.text+0x0): multiple definition of `main'
fr.o:fr.cc:(.text+0x0): first defined here
vw.o: In function `main':
vw.cc:(.text+0x0): multiple definition of `main'
fr.o:fr.cc:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'output' failed
make: *** [output] Error 1
这是我的Makefile:
output: fr.o vw.o ac.o pg.o gq.o
        g++ -o fr.o gq.o vw.o ac.o pg.o output
fr.o: fr.cc pg.h
        g++ -c fr.cc
vw.o: vw.cc ac.h
        g++ -c vw.cc
ac.o: ac.cc ac.h pg.h
        g++ -c ac.cc
pg.o: pg.cc pg.h
        g++ -c pg.cc
gq.o: gq.cc ac.h includes/bb.h
        g++ -c gq.cc -I./includes/bb.h
clean:
        -rm *.o
fr.cc, gq.cc and vw.cc all have main functions inside each of them.  
ac.cc and fr.cc both have fl(); function they have in common.
gq.cc and vw.cc both have x2(); function they have in common.

我不允许编辑下面的任何文件。目标是创建一个makefile,这样如果bb.h文件被别人修改了,makefile仍然可以工作。

下面是每个文件的文件结构:

ac.cc

#include "ac.h"
#include "pg.h"
#include <iostream>
using std::cout;
using std::endl;
void  y7(void)
{
  cout << "y7()" << endl;
}
void  x2(void)
{
  cout << "x2()" << endl;
  f1();
}

ac.h

void y7(void);
void x2(void);

fr.cc

#include <iostream>
using std::cout;
using std::endl;
#include "pg.h"
int main()
{
  cout << "program fr" << endl;
  f1();
  return 0;
}

gq.cc

#include <iostream>
using std::cout;
using std::endl;
#include "ac.h"
#include "bb.h"
int main()
{
  cout << "program gq" << endl;
  x2();
  cout << "CONST111: " << CONST111 << endl;
  cout << "CONST222: " << CONST222 << endl;
  return 0;
}

pg.cc

#include "pg.h"
#include <iostream>
using std::cout;
using std::endl;
void  f1(void)
{
  cout << "f1()" << endl;
}
void  g3(void)
{
  cout << "g3()" << endl;
}

pg.h

void f1(void);
void g3(void);

vw.cc

#include <iostream>
using std::cout;
using std::endl;
#include "ac.h"
int main()
{
  cout << "program vw" << endl;
  x2();
  return 0;
}

包括/bb.h

define CONST111   42
#define CONST222   84

变化

gq.o: gq.cc ac.h bb.h
        g++  -c gq.cc -I./includes/bb.h 

gq.o: gq.cc ac.h
         g++ -c gq.cc -I./includes 

gq.o: gq.cc ac.h includes/bb.h
         g++ -c gq.cc -I./includes 

构建失败的原因是由于bb.h不在同一目录中。您将bb.h列为需求,由于它不在那里,因此尝试生成它,但没有找到合适的规则。您要么不将bb.h列为需求,要么指定带有其路径的文件,以便make看到它存在。您还需要使用-I./includes来指定g++将在其中查找bb.h的搜索目录。

你还需要修改这个:

output: fr.o vw.o ac.o pg.o gq.o
    g++ -o fr.o gq.o vw.o ac.o pg.o output

有很多问题。首先,您试图将对象与冲突的符号联系起来,并且您使用-o标志是错误的。根据评论和你的编辑,我认为你想要的是:

output: fr.o vw.o ac.o pg.o gq.o
        g++ -o fr fr.o pg.o
        g++ -o gq gq.o ac.o pq.o
        g++ -o vw vw.o ac.o pg.o

这将生成三个二进制文件,frgqvw,以三个文件命名,并带有main()符号。每个都只列出了解析外部符号所需的对象。

我可以看到至少两个错误在这个Makefile,但它不可能告诉不知道文件布局:

以下是我的修复:

output: fr.o vw.o ac.o pg.o gq.o
        g++ -o output fr.o gq.o vw.o ac.o pg.o
fr.o: fr.cc pg.h
            g++ -c fr.cc
vw.o: vw.cc ac.h
            g++ -c vw.cc
ac.o: ac.cc ac.h pg.h
            g++ -c ac.cc
pg.o: pg.cc pg.h
            g++ -c pg.cc
gq.o: gq.cc ac.h includes/bb.h # need to give path to bb.h
            g++ -c gq.cc -I./includes
clean:
            -rm *.o

如果你仍然有问题,把你的文件布局贴在问题里。你可以这样写:

find .