gpt4 book ai didi

c++ - Makefile 头目录错误

转载 作者:行者123 更新时间:2023-11-28 06:05:54 25 4
gpt4 key购买 nike

我试图弄清楚我在使用 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

这是我的生成文件:

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

交流

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 列为要求,或者指定文件及其路径,以便 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

问题很多。首先也是最重要的是,您试图将对象与冲突的符号链接(symbolic link)起来,并且您对 -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()< 中的三个文件命名 符号。每个仅列出解析外部符号所需的对象。

关于c++ - Makefile 头目录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32419413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com