Author Topic: Programming Question: Give a C++ Class without the code  (Read 4091 times)

Aaron

  • *
  • Posts: 2818
    • View Profile
    • http://aaron-kelley.net/
Seeing as that I've been studying computer science in college for almost six months, I'm starting to know a thing or two about programming :P
I've got a question, and I haven't been able to find an answer in Visual C++'s help or on Google, but I think someone here may know how to do this...


Let's say I've written a nice class in C++ that I want to give to someone else to use in a program.  I don't want to give out the source code, I think it is possible to give the .h file with the declarations and etc. accompanied with the object code, and he should be able to use the class in his program.  I have the object code file that is generated when the program is compiled, but I don't know if you have to do something else to it to make it usable in another program, or if you have to take a different approach, because I'm not getting it to work.

DeadLajik

  • *
  • Posts: 53
    • View Profile
Programming Question: Give a C++ Class without the code
« Reply #1 on: 2004-02-08 19:13:55 »
You are right, you'd have to give out the header file and the object file (if you chose not to give out the source).  The programmer then would compile his/her source code and include your header file and add your object file to the end of the link step. It is up to the linker to add the functions/methods from your object file into the final executable.

Keep in mind though that object files created with Visual C++ will not link with other object files created with other compilers like GCC or Borland C++ or anything else except Visual C++. This is because C++ compilers mangle the names of methods that are exported (to support overloading / polymorphism) and each compiler has it's own way of doing this which is incompatible (I believe it is called the ABI - Application Binary Interface).

Aaron

  • *
  • Posts: 2818
    • View Profile
    • http://aaron-kelley.net/
Programming Question: Give a C++ Class without the code
« Reply #2 on: 2004-02-08 20:07:03 »
Yeah, I was pretty sure it would work like that.  I guess the problem is, I can't find the option in Visual C++ to add an object file into the linking process, or I don't know what I'm looking for.  I'm still looking, but if someone already knows, replies are appreciated :P

[Edit] Nevermind, I figured it out.  You have to go to properties on the project and add the filename of your object file into the "additional dependencies" for the linker input, then it works great.

Thanks :P

mirex

  • *
  • Posts: 1645
    • View Profile
    • http://mirex.mypage.sk
Programming Question: Give a C++ Class without the code
« Reply #3 on: 2004-02-09 17:49:21 »
I have did this through .lib file ... i created a LIB project ... and then result .lib file could be included in any other project.

But i would like to know, is it possible to create LIB, DLL or something so my C++ class would be usable in ... lets say ... Delphi ?

ficedula

  • *
  • Posts: 2178
    • View Profile
    • http://www.ficedula.co.uk
Programming Question: Give a C++ Class without the code
« Reply #4 on: 2004-02-09 18:13:49 »
Well, DLL's are pretty safe. If you have a DLL that exports functions, and all the parameters/return values are standard types (int, float, char*), or structs/arrays of simple types like that, it should work in practically any other language.

Of course, that doesn't let you actually export classes, just individual functions.

If you can compile to OBJ files, Delphi is perfectly happy to link into those for external functions, so that works too (if you want to avoid having separate DLLs). I don't know how many other compilers will use OBJ files, but I'd expect some would.

For exporting actual classes ... not so easy. Interestingly, Delphi can export C++ compatible class objects, but I don't know whether it can go the other way and import external classes. For that sort of thing, COM might be the best bet; most Windows languages support COM one way or another.

mirex

  • *
  • Posts: 1645
    • View Profile
    • http://mirex.mypage.sk
Programming Question: Give a C++ Class without the code
« Reply #5 on: 2004-02-09 20:27:45 »
thanks for hints ficedula ...

DeadLajik

  • *
  • Posts: 53
    • View Profile
Programming Question: Give a C++ Class without the code
« Reply #6 on: 2004-02-09 23:33:16 »
Delphi can use your functions fine, but you need to make sure you are exporting them and making them availible to be called using the standard C calling convention.

You do this by making the prototypes external like this:

extern "C"
{
   int function(int somevariable);
   void someotherfunction();
}