不确定如何创建MakeFIle

时间:2013-03-17 06:19:39

标签: c++ linux makefile

我在为包含3个c ++文件的项目创建makefile时遇到问题。

在包含文件的目录(tree.h,helperMethods.h和main.cpp)中,我首先使用命令 emacs makefile.make 。然后,在emacs中,我将makefile写为:

all: tree.h helperMethods.h main.cpp
[tab]g++ -std=c++0x -o project3 tree.h helperMethods.h main.cpp

我已手动验证命令 g ++ -std = c ++ 0x -o project3 tree.h helperMethods.h main.cpp 确实编译了这三个文件,并制作了一个可执行的可执行文件应该。

在此之后,我保存makefile,退出emacs,然后尝试运行它。

首先我使用 make ,但这返回了:

make: *** No targets specified and no makefile found.  Stop.

然后我尝试使用 make -f makefile.make ,但这也不起作用并返回:

make: Nothing to be done for `all'.

此时,我不确定为什么makefile构造不正确。我对makefile没有太多经验;我很确定我的makefile正文中的命令是正确的,但是当我编写它时我没有正确设置它。

如果完全相关,这里有3个文件:

tree.h中:

#ifndef tree_h
#define tree_h
#include <vector>

using namespace std;

template<class T>
class tree
{
public:
    tree<T> * parent;
    T element;
    vector<tree<T> > nodes;
    tree(const T& theElement)
    {
        element = theElement;
        nodes = vector<tree<T> >();
    }
    tree()
    {
        nodes = vector<tree<T> >();
    }
};

#endif

helperMethods.h

#ifndef helperMethods_h
#define helperMethods_h

#include "tree.h"

#include <tuple>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

string printName(tuple<string, string, string> myTuple){
    string ret = "";
    if(std::get<0>(myTuple).length() > 0)
        ret += get<0>(myTuple) + " ";
    if(std::get<1>(myTuple).length() > 0 || std::get<2>(myTuple).length() > 0)
        ret += "(";
    if(std::get<1>(myTuple).length() > 0)
        ret += get<1>(myTuple);
    if(std::get<1>(myTuple).length() > 0 && std::get<2>(myTuple).length() > 0)
        ret += ", ";
    if(std::get<2>(myTuple).length() > 0)
        ret += get<2>(myTuple);
    if(std::get<1>(myTuple).length() > 0 || std::get<2>(myTuple).length() > 0)
        ret += ")";
    return ret;
}

bool tupleContain(tuple<string, string, string> myTuple, string myString){
    return (std::get<0>(myTuple).compare(myString) == 0) || (std::get<1>(myTuple).compare(myString) == 0);
}

void findElement(tree<tuple<string, string, string> > myTree, string myString, bool* found, vector<int> *ret)
{
    if(tupleContain(myTree.element, myString))
        *found = true;
    if(! * found)
    {
        for(int counter = 0; counter < (int)myTree.nodes.size(); counter ++)
        {
            if(!* found)
            {
                (*ret).push_back(counter);
                findElement(myTree.nodes.at(counter), myString, found, ret);
                if(!* found)
                    (*ret).pop_back();
            }
        }
    }
}

void getLineage(tree<tuple<string, string, string> > myTree, string myString){
    bool dummyForFound = false;
    bool * found = & dummyForFound;
    vector<int> lineage = vector<int>();
    vector<int> * pointer = & lineage;
    findElement(myTree, myString, found, &lineage);
    if(lineage.size() == 0)
    {
        cout << "Species not present" << endl;
        return;
    }
    vector<string> printString = vector<string>(lineage.size() + 1);
    tree<tuple<string, string, string> > * currentNodePointer = & myTree;
    for(int counter = 0; counter <= (int) lineage.size(); counter ++)
    {
        string currentLine = "";
        for(int counter2 = 0; counter2 < 2*((int) lineage.size() - counter); counter2 ++)
            currentLine += ">";
        if(counter != lineage.size())
            currentLine += " ";
        tree<tuple<string, string, string> > currentNode = * currentNodePointer;
        currentLine += printName(currentNode.element);
        if(counter < (int) lineage.size())
        {
            int foo = lineage.at(counter);
            tree<tuple<string, string, string> > currentNodeDummy = currentNode.nodes.at(foo);
            *currentNodePointer = currentNodeDummy;
        }
        printString.at(counter) = currentLine;
    }
    for(int counter = 0; counter < (int) printString.size(); counter ++)
        cout << printString.at(printString.size() - counter - 1) << endl;
    cout << endl;
}

void getCommonLineage(tree<tuple<string, string, string> > myTree , string name1, string name2)
{
    bool dummyForFound = false;
    bool * found = & dummyForFound;
    vector<int> lineage1 = vector<int>();
    vector<int> * pointer1 = & lineage1;
    vector<int> lineage2 = vector<int>();
    vector<int> * pointer2 = & lineage2;
    findElement(myTree, name1, found, pointer1);
    * found = false;
    findElement(myTree, name2, found, pointer2);
    if(lineage2.size() == 0 || lineage1.size() == 0)
    {
        cout << "At least one species not present." << endl;
        return;
    }
    bool stillSame = lineage1.at(0) == lineage2.at(0);
    cout << "Level[0] Common Ancestor: ROOT (ROOT, ROOT)" << endl;
    tree<tuple<string, string, string>> * lastSharedNode = & myTree;
    int finalCounter = 0;
    for(int counter = 0; counter < (int) min(lineage1.size(), lineage2.size()) && stillSame; counter ++)
    {
        tree<tuple<string, string, string> > dummyNode = * lastSharedNode;
        tree<tuple<string, string, string> > currentNode = dummyNode.nodes.at(lineage1.at(counter));
        *lastSharedNode = currentNode;
        if(counter < (int) min(lineage1.size(), lineage2.size()) - 1 && lineage1.at(counter + 1) != lineage2.at(counter + 1))
            stillSame = false;
        tuple<string, string, string> currentElement = currentNode.element;
        cout << "Level[" << counter + 1 << "] Commont Ancestor: " << printName(currentElement) << endl;
        finalCounter ++;
    }
    cout << endl;
    cout << "Ancestry unique to " << name1 << endl;
    tree<tuple<string, string, string> > savedNode = *lastSharedNode;
    tree<tuple<string, string, string> > * currentUnsharedNode = lastSharedNode;
    for(int counter = finalCounter; counter < (int) lineage1.size(); counter ++)
    {
        tree<tuple<string, string, string> > dummyNode = * currentUnsharedNode;
        tree<tuple<string, string, string> > currentNode = dummyNode.nodes.at(lineage1.at(counter));
        tuple<string, string, string> currentElement = currentNode.element;
        *currentUnsharedNode = currentNode;
        cout << "Level[" << counter + 1 << "] ";
        if(counter == lineage1.size() - 1)
            cout << "Species of interest: ";
        cout << printName(currentElement) << endl;
    }
    cout << endl;
    currentUnsharedNode = &savedNode;
    cout << "Ancestry unique to " << name2 << endl;
    for(int counter = finalCounter; counter < (int) lineage2.size(); counter ++)
    {
        tree<tuple<string, string, string> > dummyNode = * currentUnsharedNode;
        tree<tuple<string, string, string> > currentNode = dummyNode.nodes.at(lineage2.at(counter));
        tuple<string, string, string> currentElement = currentNode.element;
        *currentUnsharedNode = currentNode;
        cout << "Level[" << counter + 1 << "] ";
        if(counter == lineage2.size() - 1)
            cout << "Species of interest: ";
        cout << printName(currentElement) << endl;
    }
    cout << endl;
}
#endif

的main.cpp

#include "rapidxml.h"
#include "tree.h"
#include "helperMethods.h"

#include <string>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>

using namespace rapidxml;

int main(int argc, const char * arv[]){
    tuple<string, string, string> human ("Human", "Homo sapiens", "Species");
    tuple<string, string, string> apes ("Apes", "", "");
    tuple<string, string, string> dogs ("Dogs", "", "");
    tuple<string, string, string> root ("Root", "", "");
    tuple<string, string, string> bears ("Bears", "", "");
    tuple<string, string, string> cat ("Cat", "", "");
    tuple<string, string, string> horse ("Horse", "", "");
    tree<tuple<string, string, string> > myTree = tree<tuple<string, string, string>>(root);
    tree<tuple<string, string, string> > b = tree<tuple<string, string, string>>(dogs);
    tree<tuple<string, string, string> > c = tree<tuple<string, string, string>>(apes);
    tree<tuple<string, string, string> > d = tree<tuple<string, string, string>>(bears);
    tree<tuple<string, string, string> > e = tree<tuple<string, string, string>>(horse);
    tree<tuple<string, string, string> > f = tree<tuple<string, string, string>>(human);
    tree<tuple<string, string, string> > h = tree<tuple<string, string, string>>(cat);
    d.nodes.push_back(f);
    e.nodes.push_back(h);
    b.nodes.push_back(d);
    b.nodes.push_back(e);
    myTree.nodes.push_back(b);
    myTree.nodes.push_back(c);
    cout << printName(myTree.nodes.at(0).element);

    cout << "Welcome to my Tree of Life program!" << endl << endl;

    int choice = 1;
    while(choice == 1 || choice == 2) {
        cout << "Please choose from the following options:" << endl;
        cout << "   1.Get the lineage of a species" << endl;
        cout << "   2.Get the commmon lineage of two species" << endl;
        cout << "   3.Exit program" << endl << endl;
        cin >> choice;
        cout << endl;
        if(choice == 1)
        {
            cout << "Please enter the name of the species of interest:" << endl;
            string name;
            cin >> name;
            cout << endl;
            getLineage(myTree, name);
        }
        if(choice == 2)
        {
            string name1, name2;
            cout << "Please enter the name of the first species: " << endl;
            cin >> name1;
            cout << "Please enter the name of the second species: " << endl;
            cin >> name2;
            getCommonLineage(myTree, name1, name2);
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:3)

Makefile通常被称为Makefile(虽然makefile也可以)但没有任何扩展名。如果您使用扩展名(不推荐),则必须告诉make makefile的名称。这很乏味也没必要。

其次,您不需要将头文件放在编译命令行中,也不应该这样做。您在文件中使用#include行来指定头文件。所以你的编译命令可能如下所示:

g++ -std=c++0x -o project3 main.cpp

现在,在那个命令中:

  • main.cpp来源

  • project3目标(即将要创建的文件。)

此外:

    编译工作需要
  • tree.hhelperMethods.h;编译取决于这个文件。 (你知道,但它们并没有出现在命令行中,所以它并不明显。)此外,如果它们发生了变化,你必须重新编译你的文件。

makefile解释了如何从创建目标,还列出了目标依赖项 >。 (从技术上讲,make不区分 sources 依赖;它认为它们都是先决条件。但是识别差异很方便。)

因此对于上面的命令,整个make配方可能如下所示:

project3: main.cpp tree.h helperMethods.h 
        g++ -std=c++0x -o project3 main.cpp

通常,我们不使用main.cpp之类的文件名;相反,我们将调用project3 project3.cpp的主文件,就像目标(输出)文件是project3一样。事实上,如果你这样做,并且你没有任何其他依赖项(头文件),你可以输入:

make project3

根本没有makefile ,而make会提供命令:

g++ -o project3 project3.cpp

在这种情况下,该命令会出错,因为它没有指定正确的C ++标准。但它通常有效,并且使用相同的基本名称命名目标和来源是一个很好的理由。

还有一件事:您的文件tree.h是一个仅限标题的库,这很好。但是helperMethods.h只是伪装成头文件。这真的是一个完整的实现。你应该解决这个问题。

此外,将using namespace std放入头文件中确实是个坏主意。头文件应该在需要它的所有内容上明确使用std::命名空间前缀。通常不建议在任何地方使用using namespace std,但它在头文件中特别糟糕,因为它会静默地污染包含标头的任何文件的默认命名空间。这可能会导致非常模糊的错误或编译错误。

答案 1 :(得分:1)

学校项目,嗯?对于你自己正在做的事情,project3似乎是一个可怕的任意名称。

您不需要在makefile处理中包含.h文件,因为严格来说,它们永远不需要独立编译。你的makefile需要一个规则来构建main.co中的main.o,然后是一个用于从main.o构建可执行文件的规则。头文件应该作为main.o的依赖项添加,这样如果其中任何一个发生更改,make将知道从那里重建。

基本上,它需要看起来像这样:

# http://stackoverflow.com/questions/15458126/unsure-how-to-create-a-makefile
all: project3

clean:
    -rm main.o project3

project3: main.o
    g++ -std=c++0x -o project3 main.o

main.o: main.cpp tree.h helperMethods.h
    g++ -std=c++0x -c main.cpp

为此目的,其他人可能会生成一个截然不同但同样有效的makefile。这个过程有一定的自由度。

你在这里有一个规则来构建整个事物(请记住,如果没有提供目标,makefile中的第一个是自动选择的),清理任何中间文件的规则,构建来自目标文件的可执行文件,以及从源文件构建目标文件的规则。这种方法不能很好地扩展;对于更大,更复杂的makefile,手动管理所有依赖项会很不愉快,但在这个非常简单的情况下它很好。