tags:

views:

85

answers:

4

I'm not sure if I am going about this the right way. I am making some c++ classes to use in 2 apps. I am going to have to compile them to be used in a Cocoa app and later be compiled for use with fastcgi.

Should I create a dynamic library?

A: 

If you want to share multiple C++ class among projects you should typically place them in a class library. Not familiar with Cocoa though.

Curtis White
What file extension is a class library?
joels
+5  A: 

If the source files have to be compiled with different conditional compilation settings to work in the two applications, you'd better ship the source and compile it along with the apps themselves.

Otherwise, you can create a library and ship the compiled versions along with the headers for compilation of the apps. Whether the library should be dynamic or not depends on your situation. If you don't need to update the library separately without recompiling the executable, a simple static library is probably a better choice.

Mehrdad Afshari
+3  A: 

Don't forget that you have static library as an option too. On some platforms dynamic libs come with a bunch of annoying baggage. They're also slightly slower (though this is usually beside the point). They also can't be replaced without recompiling the program (you might be more concerned about this in a web environment). Finally, when you're only running one of the programs on a computer you gain nothing by making the lib dynamic...and little when there's only two.

Noah Roberts
So, if I make a static library, it would contain the compiled executables? I would then include the header files with it? This would allow me to include the libs and header files in my application projects?
joels
Well, static libraries are not generally executable but they would contain the compiled object code for the stuff you put in them. The rest of your questions can be answered in the affirmative.
Noah Roberts
"compiled object code" that is what I meant. Thanks.A static library sounds a lot like a Framework in xcode. When I create a Framework in xcode, the framework file is like a folder/package and it has a Headers folder in it for the public headers. When I create a Static Library in Xcode, it creates a .a file. When I want to use the lib, how do I include the headers? Do I just include the lib and any headers? #include libTest.a #include MyClass.h
joels
static libraries generally work just like dynamic libraries except that they are linked at compile time and included in the executable. I don't know anything about this Framework.
Noah Roberts
A: 

If you have many classes, then shared library. Make sure to use only abstract classes and no code in public headers (templates are OK). Provide factories (or plain functions) to instantiate the objects.

If that sounds like too much coding for you then, well, modern version control makes it rather painless to simply re-use the files in several projects, living in the same repository.

P.S. Another factor to consider is how many people work on the project. Shared library is an extra responsibility. If you have a person to take care of it, then it might be worth doing simply from organizational point of view.

Dummy00001