Making and using your own library

Hey all,

I've been trying for some time now to make my own first library. After going through whatever I could find online I've gotten this far:

testlib.h
1
2
3
4
5
6
#ifndef _testlib_h
#define _testlib_h

void HelloWorld();

#endif 


testlib.cpp
1
2
3
4
5
6
7
8
#include<iostream>
#include "testlib.h"
using namespace std;

void HelloWorld() {
cout << endl << "Hello World" << endl;

}


test.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "testlib.h"
using namespace std;


int main ()  {

HelloWorld();

return 0;	
}


I've compiled testlib.cpp to create a testlib.o file, but when I try to compile test.cpp I get

Undefined symbols:
  "HelloWorld()", referenced from:
      _main in ccueOlJg.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


(Every time I try to compile, the "ccueOlJg.o" comes out different; apparently always in the format "cc******.o".)

Any suggestions please?

So far, I've used only a simple text editor and Terminal, and I'd very much appreciate to learn how to do this without having to use fancier programs.

By the way, I'm using Mac OS X 10.6.8.


Thanks in advance! :)
Last edited on
Hello again!

As I'm also new to this forum, I'm not sure why I got a response to my question by email that is not visible in this thread... Anyway, I was told

Make sure that when you build, you tell your linker to use the
testlib.o file.


I see that maybe I should also mention that I use g++ for compiling and executing,
and the commands used thus far were

g++ -c testlib.cpp -Wall

to create the .o-file and then
 g++ test.cpp -Wall

- the latter produces the mentioned error.

I'm not sure how to execute the response quoted above...?

Again, thanks in advance!
You need 2 projects. The library project you put testlib and the other (console) project you need to tell the ide or makefile that it has to take the lib (*.a) from the output of the library project.
Topic archived. No new replies allowed.