Making my own C++ Library
In order to learn, I am creating my own very elemental C++ library as follows:
Steps:
1) Create the files "mylibrary.h" and "mylibrary.cpp".
2) Only compile the library, and it shouldn't have a "main()" function in it. This step will generate an object file called "myfile.o".
3) Create another file called something like "main.cpp". In main.cpp you need to #include "mylibrary.h".
4) Compile main.cpp and add the library from step 2 to link in (in this case "myfile.o"). If you don't add this library, you'll get a error message like "linker error - can't find function definition", or something like that.
This is my code for mylibrary.h:
/* Program name: mylibrary.h ============================================== Inclusions: */ #include <iostream> using namespace std; void testfunction(){ //simple function code: cout<<"This is a function"<<endl; }
And this is my code for mylibrary.cpp:
/* Program name: mylibrary.cpp ============================================== Inclusions: */ #include <iostream> using namespace std; void testfunction(){ //simple function code: cout<<"This is a function"<<endl; }
Question 1: Which XCode file tool do I use to create mylibrary.h? I tried the New File > C and C++ Header File, but then it won't allow me to compile it (the compile function is dim)
Question 2: Why did I have to create mylibrary.cpp if I didn't use it?
Maybe one of you kind folks can outline the steps a little more clearly, showing which tools to use.
Thanks for your help amigos - Migs
Dual 2 GHz PowerPC G5/ 5.5 Gb SDRAM, Mac OS X (10.5.5), Using C++ under XCode 3.0