Makefile头目录错误
Makefile header directory errors
我正试图找出我们的错误,我一直得到与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
这将生成三个二进制文件,fr
、gq
和vw
,以三个文件命名,并带有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 .
相关文章:
- 我将 Makefile 更改为 C++11 版本,但仍然收到列表初始值设定项错误
- 在 Windows 10 下为 VTK 生成 Makefile 时,Qt mkspecs 的路径错误
- 内核.cpp在制作 kernel.o 时显示错误和 Makefile 错误
- 从 win 7 升级到 Win 10 后,出现 Dev-C++ makefile.win 编译错误
- makefile和错误将与大型项目分开的文件编译
- cyttsp4 (linux kernel) Makefile 中的错误
- G makefile错误: *.o:没有这样的文件或目录
- 使用 makefile 生成项目时出现多个定义错误C++
- makefile 和 lex+yacc 中的错误 -> 错误:"noyywrap"未声明(在此函数中首次使用)
- makefile错误没有这样的文件或目录
- 获取错误makefile:45:条件中的语法无效.停止
- C++ 编译错误:- 使用Makefile对"main"进行多重定义
- 在makefile中使用退出命令会导致编译错误
- 在Ubuntu 16.04上编译PCL 1.7,CMake生成的Makefile中出现错误
- 比特币,MinGW,Windows 7,Makefile错误
- Dev-C++给出 Makefile.win 和 g++ 停止的错误
- Mingw makefile 编译错误窗口
- 一个简短的c++文件和makefile:我可以在shell中制作,但在Eclipse中构建它时会遇到很多错误
- 如何修复Makefile.am星号Cpp:C++静态库中的错误
- 未定义的引用错误Makefile c++