繁体   English   中英

Makefile标头目录错误

[英]Makefile header directory errors

我试图弄清楚我在make上遇到的错误。 我有几个源文件和一个includes目录(包含bb.h )一起位于assign1目录中。 我正在尝试做的是在创建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仍将起作用。

这是每个文件的文件结构:

交流

#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();
}

交流电

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);

大众网

#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 ,请尝试生成它,但找不到合适的规则。 您无需将bb.h列为要求,也无需指定文件及其路径,以使该文件存在。 您还需要只使用-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 .

在您的项目文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM