0

I want to know if i can link and compile source code file with an object file of a class without including class source files.

Or

Like in Java i can give a compiled *.class file to a friend and the doc of that class without giving the source code and he can include that class for his need without knowing the real source.

Can i do the exact thing with C++ ? and How ?

FYI:I'm using MinGW.

I'm quite new to so this may look like a dumb question (but still there's no dumb questions).

4 Answers 4

1

In C++ you can just produce an executable file (.exe in windows, usually no extension in Linux) or a library file (.lib or .a) or a shared libarary (.dll or .so) depending on what you want to achieve.

If you are providing a library, you will have to provide a header file with the class declarations as well. If you don't want to give out too much about the details, you can have a "implementation" class that is simply a pointer to the implementation in your header file, and the real implementation is only available to you as the owner of the sources.

For more information, you should look up "PIMPL" as Thomas Matthews suggested - that's short for "Pointer to IMPLementation class". Basically, your "public" class is just a shell providing the functions you want others to see, and the implementation class inside it does all the hard work.

Simple example

In your header file:

// Forward declaration of the implementation class. 
class double_it_impl;

// Class that stores an integer, and doubles it each time you 
// call doubling().
class Public_double_it
{
  public:
    public_double_it(int x);    // COnstructor. 

    int doubling();        // Function d

  private:
     double_it_impl *pImpl;
};

In your source or private header file, we declare the actual implementation:

class double_it_impl
{
  public:
    double_it_impl(int x) : m_x = x; {};
    int doubling()  { m_x *= 2; return m_x; }

}


public_double_it::public_double_it(int x)
{
    pImpl = new public_double_it(x);
}

int public_double_it::doubling()
{
   return pImpl->doubling();
}

Now you can't see what the implementation class contains or how it works - of course, this is a very trivial example, but I hope it conveys the idea.

3
  • How can i create an Implementation class ? Jan 13, 2013 at 19:58
  • In Java parlance, what you describe is an Interface. You can do the exact same thing in C++ by only declaring pure virtual functions in your class. No need for icky pointers.
    – JvO
    Jan 13, 2013 at 20:20
  • Right, so an interface isn't a PIMPL - an interface provides a class that has no implementation, that your code can have pointers to via some "factory" function. A pimpl is a "complet" class, but the actual implementation is hidden. Yes, the difference isn't very large, but there is a difference. Jan 13, 2013 at 20:43
0

You can give them the *.class but they can be decompiled.

Most libraries are provide using the classes and the generated Javadoc.

5
  • is there any way to give them something so that they can use but not see the source code with C++ ? Jan 13, 2013 at 19:32
  • With C++ it is harder to reverse engineer but not impossible. The only way to prevent access to the code is to run the code for them on their behave via a service. Jan 13, 2013 at 19:37
  • Ok thanks but still how can i give them compiled binary even though it can be reversed engineered ? Jan 13, 2013 at 19:40
  • I would worry more about something which works. The simplest way in Java is to create a jar. Maven does this very simply. You can add an obfuscator to make the code more difficult to disassemble. Jan 13, 2013 at 19:44
  • Something like that for C++ ? Jan 13, 2013 at 19:50
0

You could compile the class into a library or dll or shared library and give to your friend.

They would need a header file that defines the interface.

See also PIMPL idiom.

0

Yes, you can provide a compiled object file and documentation and others can link the .o (or .obj) file into their code. The exact details of how to do this vary between compilers and development environments. If you are using g++, you simply use the following command-line:

g++ -c source.cpp

This will create a .o file which you (or anyone else) can then link with other .o files with

g++ main.o source.o

Other compilers will have similar options. Read the help files and other documentation for instructions about how to create object files.

For a large number of functions and/or classes, you should look at creating a library file. These can be statically or dynamically linked. I suggest that you search the Web for more information if you want to learn about how to create your own library files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.