非常非常基本:为什么我的makefile不起作用

时间:2017-02-23 07:04:08

标签: c++ linux makefile gnu-make

himake: hello.o printing.o name.o
    g++ -o himake hello.o printing.o name.o

hello.o: hello.cpp
    g++ -c hello.cpp

printing.o: printing.cpp
    g++ -c printing.cpp

name.o: name.cpp
    g++ -c name.cpp

运行上面的makefile会在下面给出以下错误:

[alex @ pcc Dir] $ make g ++ -o himake hello.o printing.o name.o

hello.o:在函数`main'中:

hello.cpp :(。text + 0xc8):对`printHello(std :: basic_string,std :: allocator>)'的未定义引用

collect2:ld返回1退出状态

make:*** [himake]错误1

文件: HELLO.CPP:

// hello.cpp

// standard library
#include <iostream>
#include <string>
using namespace std;

// user defined header files
#include "name.h"
#include "printing.h"

int main ()
{
    string name;

    name = getName();   // getName is in name.h
    printHello(name);  // printHello is in print.h

    return 0;
}

name.cpp

// name.cpp

// user defined header files
#include "name.h" 
#include "printing.h"

string getName()
{
    string name;
    printGreeting();    // printGreeting is from print.h
    getline(cin, name);  
    return name;
}

name.h

// name.h

#include <iostream>
using namespace std;

string getName();

printing.cpp

// printing.cpp

// user defined include files
#include "printing.h"

void printGreeting(void)
{
    cout << "Your name: ";
    return;
}

void printHi (string  name)
{
    cout <<  "Hi, " << name << endl;
    return;
}

printing.h

// printing.h

#include <iostream>
using namespace std;

void printGreeting();
void printHello(string);

1 个答案:

答案 0 :(得分:1)

您宣布 class MyPager extends PagerAdapter { int size; ArrayList<GridView> GridArrayList; public MyPager( ArrayList<GridView> GridArrayList) { size=GridArrayList.size(); this.GridArrayList = GridArrayList; } @Override public int getCount() { return size; } public Object instantiateItem(ViewGroup container, int position) { Log.d("position", "" + position); GridView gridView = GridArrayList.get(position); container.addView(gridView); return gridView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager)container).removeView((GridView) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((GridView) object); } }

printHello

...然后你调用它,而不是定义它。如果没有定义,链接将失败。 (这与makefile无关。)

print.cpp中定义的void printHello(string); 函数可能是printHi的定义。