Making my own C++ Library

Friends:
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

Posted on Dec 5, 2008 12:02 PM

Reply
6 replies

Dec 9, 2008 1:14 PM in response to etresoft

Hi etresoft:

I try using a static library tool and Xcode opens with a file ending in extension .a Then I add a C header file and include my code and compile it. It compiles, but I cannot see the products of the compilation, and cannot find the much needed object file ending in .o

Also the initial file ending in extension .a cannot be edited.

Thanks for your comments! -Migs

Dec 9, 2008 1:54 PM in response to Miguel Reznicek

A .o file is an object file. It is an intermediate file that the linker uses to build a final executable. You normally don't do anything at all with .o files. If you are building small projects, you may not have any .o files at all.

Could you explain a little bit about what exactly you want to do? Perhaps you really don't need a library after all. I'm not really sure.

Normally, when writing an application, you have a set of source code files. One of those should contain a main() function. If you want to use code defined in a library, you will #include the appropriate header files from the library. The easiest way to do this on MacOS X is with frameworks, which takes care of the details for you. If not using frameworks, you can just tell Xcode which static or dynamic library you want to link with, providing details like the link path and search path. Then Xcode will compile your code into .o files and link them with all the libraries you have specified.

Dec 10, 2008 4:31 AM in response to etresoft

Hi etresoft:

First of all thanks for helping me. It means a lot! I am a 47 yrs old engineer who is trying to get proficient at C++ for several of my simple robotics projects. What I am trying to do is make a library of my own functions so I can include it whenever I want to. By learing with a small code example, I will then have the know-how to make a much better library. (Thus my extremely simple code)
In dream.in.code I found the steps outlined in my first post so I decided to follow that strategy. Probably due to Xcode differences I am not exactly being able to emulate the results of those steps.

Perhaps you can take that simple code I placed in there and outline the steps and Xcode tools that I should use to create the library, finally ending in an example where I include and call my new library. This would be very helpful. I know it means time for you so I would like to repay you kind gesture somehow. (I'm thinking I could PayPal you enough for a nice bottle of Bourbon or something else that you like)

Thanks in advance, -Migs
Miguel Reznicek
mreznicek@pretensa.com

Dec 11, 2008 9:41 PM in response to Miguel Reznicek

Migs,

I think this is one way to do what you're trying to do. I started with an empty Xcode project and it seemed to work OK for this simple example. In general though, if you don't understand all the various build settings, I think it can get complicated pretty quickly if you try to manually create complex projects starting from an empty project. I'm did this in Xcode 3.1.2.

*_Create an empty Xcode Project_*
Launch Xcode and "File -> New Project..." to create a new "Empty Project". At this point you'll have no source files and no targets in the project.

*_Create a new target for building your library_*
Do "Project -> New Target...". Select "BSD" and then select "Object file". Name the target "MyLibrary" and click "Finish". In the "Groups & Files" pane of your Xcode window you should now have a "MyLibrary" entry under the "Targets" group and under the top project icon you should have a "Products" folder that shows a "MyLibrary.o" item (this will be colored red since it doesn't exist yet).

*_Create the source files for your library_*
Select "File -> New File...", selecte "C and C++" and then "C++ File". Click "Next". Name your file "MyLibrary.cpp" (and check the checkbox to "also create MyLibrary.h"). Make sure the checkbox is checked to add the files to the "MyLibrary" target. Click "Finish"

You could now paste the code from your original post into the .h and .cpp files... Except the code you posted for the .h file was not quite right. You posted:
Miguel Reznicek wrote:
/*
Program name: mylibrary.h
==============================================
Inclusions: */

#include <iostream>
using namespace std;

void testfunction(){ //simple function code:
cout<<"This is a function"<<endl;
}</div>
But your .h file typically should just contain prototypes for your library functions and not executable statements (so the "cout" code shouldn't be there... it goes into your .cpp file). You probably want something like this:

#include <iostream>
using namespace std;
void testfunction();

The code you posted for your .cpp file is ok except that it should include your "MyLibrary.h" (but that include is typically inserted automatically for you when you use Xcode's "New file..." to create the .cpp and .h files. Like this:

#include "MyLibrary.h"
#include <iostream>
using namespace std;
void testfunction(){ //simple function code:
cout<<"This is a function"<<endl;
}


Once you've save the two files you should be able to build your "MyLibrary" target and the "MyLibrary.o" product should change from red to black indicating your library was created. You won't be able to run anything however because you don't have a "main" function and you're not building a complete application yet.

*_Create a second new target for building your application_*
Do "Project -> New Target...". Select "BSD" and then select "Shell Tool". Name the target "MyApp" and click "Finish". In the "Groups & Files" pane of your Xcode window you should now have a "MyApp" entry under the "Targets" group and under the top project icon you should have a "Products" folder that shows a "MyApp" item (this will be colored red since it doesn't exist yet).

*_Set up the application target so it is dependent on and links with your library_*
Single click the "MyApp" target in the Groups & Files pane to select it and then click the "Info" button in the toolbar.

In the "General" pane of the info window click the "+" button under "Direct Dependencies". Select "MyLibrary" and then click "Add target". Doing this make your "MyApp" target dependent on the "MyLibrary" target... this tells Xcode that if you attempt to build your "MyApp" then Xcode will check to see if the library has changed and rebuild it first if needed.

Now click the "+" button under "Linked Libraries". Select "MyLibrary.o" and click "Add". Doing this causes your app to link with your library's .o (object file) when you build your app.

*_Create a source file for your application_*
Select "File -> New File...", selecte "C and C++" and then "C++ File". Click "Next". Name your file "main.cpp" (at this point you don't need a .h file for "main" so you can uncheck the checkbox). And in this case make sure the checkbox is checked to add the files to the "MyApp" target (not "MyLibrary"). Click "Finish". Open up "main.cpp" and paste in the code for your "main()" function. You main file should include your library's .h file as shown below.

#include <iostream>
#include "MyLibrary.h"
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World! ";
testfunction(); // Here's the call to your library function
return 0;
}


You can now switch Xcode's "Active Target" to "MyApp" and you should be able to build and run the application. The output should show the "Hello, World!" from the main function as well as the "This is a function" output from the call to your library.

Steve

Dec 12, 2008 2:09 AM in response to Steve Herman1

Hi Steve:

It is nice to hear you again! You put together quite a tutorial for me. I'm going to do it carefully and study it with great attention. Obviously there are steps there that I don't yet understand, but I presume with a little study it will all fall into place. You are outstanding. -Migs

Steve:
e-mail me mreznicek@pretensa.com
Miguel Reznicek

Message was edited by: Miguel Reznicek

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Making my own C++ Library

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.