"Undefined Reference" error when trying to link to external library

Hello, so I'm trying to learn how to use external functions and wrote the following dummy function:

1
2
3
4
5
6
7
8
9
10
11
#include "vertex.h"
#include <iostream>
using namespace std;
using namespace MTKpp;
int main()
{
        vertex* a;
        a->setName("blah");
        cout << a->getName() << endl;
        return 0;
}


But when I try to make this file, I get "undefined reference" errors

1
2
main.cpp:(.text+0x4c): undefined reference to `MTKpp::vertex::setName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x74): undefined reference to `MTKpp::vertex::getName()'


and this is what my makefile looks like:

1
2
3
4
5
6
7
8
all: run
run: main.o
        g++ main.o -o run
main.o: main.cpp
        g++ -I/home/me/Desktop/me/mtkpp/src/Graph -L/home/me/Desktop/me/mtkpp/src/Graph/.libs/ -lGraph -c main.cpp

clean:
        rm -rf *o run 


I have a hunch that something is wrong with my makefile, since it is my first time attempting this. But the vortex.h is in /home/me/Desktop/me/mtkpp/src/Graph and the graph library is in /home/me/Desktop/me/mtkpp/src/Graph/.libs/. So if I understand this correctly, -L in conjunction with -l will tell my compiler to look for the libGraph.a/libGraph.so inside the -L library directory, and the -I I put in there so my compiler could find the "vortex.h" file. But alas, it doesn't work. And since vertex.h only includes

1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map> 


I assume g++ knows where to look for them without needing me to specify it in the makefile?
Last edited on
Your makefile does look confused. You've put the linking details in your instructions of how to make main.o - making main.o is what you do before the linking (-c is the switch that actually explicitly tells your compiler NOT to do any linking). The linking details should go in the instructions of how to make the output executable.
Last edited on
So did you mean something like this:
1
2
3
4
5
6
7
all: run
run: main.o
        g++ -L/home/me/Desktop/me/mtkpp/src/Graph/.libs/ -lGraph main.o -o run
main.o: main.cpp
        g++ -I/home/me/Desktop/me/mtkpp/src/Graph -c main.cpp
clean:
        rm -rf *o run 

This makes sense to me because the -c suppresses the linking so it won't complain about missing libraries, and then it will resolve everything at the run: main.o step. But it is still giving me
1
2
3
4
5
6
main.o: In function `main':
main.cpp:(.text+0x19): undefined reference to `MTKpp::vertex::vertex()'
main.cpp:(.text+0x50): undefined reference to `MTKpp::vertex::setName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x78): undefined reference to `MTKpp::vertex::getName()'
main.cpp:(.text+0xb9): undefined reference to `MTKpp::vertex::~vertex()'
main.cpp:(.text+0xff): undefined reference to `MTKpp::vertex::~vertex()'

when I make it. So I'm wondering if this looks right or if I am missing something subtle still?
Last edited on
Topic archived. No new replies allowed.