2015-09-06 95 views
0

我想弄清楚我一直在使用的错误。我有几个源文件位于assign1目录以及includes目录(包含bb.h)。我正在尝试的是在创建gq.o文件时包含bb.h头文件。所有其他目标文件编译除了gq.o精细由于这个错误:Makefile头文件目录错误

[email protected]:~/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 
+0

这是错误的:'-I /包括/ bb.h',应为只对* *目录,而不是文件:'-I/includes'。当然'bb.h'必须存在于那个目录中。 – Galik

+0

确定我固定的问题,你的建议,现在我得到:G ++ -o fr.o gq.o vw.o ac.o pg.o输出 G ++:错误:输出:没有这样的文件或目录 的Makefile:12 :配方目标 '输出' 失败 化妆:*** [输出]错误1 – user3325133

+0

**变化:**'克++ -o fr.o gq.o vw.o ac.o pg.o output' **到:**'G ++ -o输出fr.o gq.o vw.o ac.o pg.o' – Galik

回答

0

我可以看到在这个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 . 

在你的项目文件夹。

+0

我加了find。上面的信息,如果你能帮我弄清楚,我会很感激。 – user3325133

+0

@ user3325133您应该将您的文件结构添加到原始问题中。我已经修改了我的答案,以便使用您发布的文件结构进行构建。我错过了将完整(相对)路径放到'bb.h'。 – Galik

+0

我已经为每个文件添加了文件结构,你能帮我获得makefile来编译吗? – user3325133

1

变化

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作为要求列出,或者将路径指定为文件,以便查看它是否存在。您还需要只使用-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()符号命名。每个仅列出解析外部符号所需的对象。

+0

我做了改变,现在另一个错误:G ++ -c fr.cc G ++ -c vw.cc G ++ - c ac.cc g ++ -c pg.cc g ++ -I./includes -c gq.cc g ++ fr.o gq.o vw.o ac.o pg.o -o output gq.o:In function 'main': gq.cc:(.text+0x0):多重定义'main' fr.o:fr.cc :(文本+为0x0):此处首先定义 vw.o:在函数'主 ': vw.cc:(.text+0x0):'主要的多个定义' FR的.o:fr.cc :(文本+为0x0):第一这里定义 collect2:错误:LD返回1个退出状态 生成文件:12:配方目标 '输出' 失败 化妆:*** [输出]错误1 – user3325133

+0

我发布了更新的问题,你能帮我解决这个问题吗? – user3325133

+0

非常感谢,它编译完美。只是一个简单的问题,如果有任何源文件发生更改,make会再次工作吗?当它编译时,如何输入make clean文件fr,gq和vw仍然说。这不应该被清除,如果没有,如何删除这些文件? – user3325133