自定义库中找不到功能

时间:2013-02-08 05:48:40

标签: c++ header-files

我有一个测试文件包含的自定义库(和相应的cpp文件)。当我尝试在测试文件中调用该函数时,它给出了错误“未定义的引用< function name>”。我在将文件放入库文件方面不是很有经验,所以任何帮助都表示赞赏。

input.h

#ifndef LOC_H
#define LOC_H
#include<vector>
struct loc{
    int room, row, col;
    char c;
    bool stacked;
    //loc *north, *east, *south, *west;
};
#endif
void Input(std::vector<std::vector<std::vector<loc> > > &b, loc & start);

input.cpp

#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<getopt.h>
#include "input.h"

using namespace std;

void Input(vector<vector<vector<loc> > > &b, loc & start) {
    //Do stuff
}

TEST.CPP

#include<iostream>
#include "input.h"
#include<vector>

using namespace std;

int main(int argc, char* argv[]) {
    vector<vector<vector<loc> > > building;
    loc start = {0, 0, 0, '.', false};
    Input(building, start);
}

1 个答案:

答案 0 :(得分:0)

根本没有涉及图书馆。您只需要在链接时链接所有源文件的目标文件。最简单的方法是从源代码编译它:

g++ -o test test.cpp input.cpp

当你有一个更大的项目时,你可能想要单独编译,由makefile或脚本控制。

g++ -c test.cpp
g++ -c input.cpp
g++ -o test test.o input.o

这看起来有点笨拙,但展示了在幕后做了什么。