0

I have a custom-defined library (and corresponding cpp file) included by a test file. When I try to call the function in the test file, it gives me the error, "Undefined reference to < function name>." I'm not very experienced with putting stuff in library files, so any help is appreciated.

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);
}
camdroid
  • 524
  • 7
  • 24
  • Looks like you're not linking it properly, but whatever it is, it's probably in http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Feb 08 '13 at 05:50
  • guesses.. 1. you're not linking to it? 2. you're not linking to it in the right order? – thang Feb 08 '13 at 05:50
  • Do you know how to link it correctly? I just looked through there, and I can't see anything I specifically did wrong or should have done another way. – camdroid Feb 08 '13 at 05:56
  • 1
    How do you compile and link your program? – billz Feb 08 '13 at 06:01
  • Never mind, figured it out - I was trying `g++ test.cpp -o test`. When I did `g++ test.cpp input.cpp -o test`, it worked. Thanks for your help! – camdroid Feb 08 '13 at 06:10
  • In gcc it would be: "g++ test.cpp input.cpp -Wall". If you are on Windows using VC or something similar, make sure that both cpp files belong to the project. – comocomocomocomo Feb 08 '13 at 06:12

1 Answers1

0

There is not library involved at all. You just need to link the object files of all source files when you are linking. The easiest way is to compile it from source:

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

When you have a larger project you might want to compile separately, controlled by a makefile or script.

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

This looks a bit clumsy, but shows what is done behind the scenes.

harper
  • 13,345
  • 8
  • 56
  • 105