简体   繁体   中英

C/C++ - Hide C or C++ function code from other people

I've got a project to write a function for performing certain operations on some data. I can write the code in C/C++ but I don't want to share the code of that function with the employer. Instead, I just want to give him access to calling the function in his own code. Is it possible? I thought of these 2 approaches -

  • Hosting the function on some server (if that's possible) and giving the employer a URL to access it.
  • Password protecting the function code file (again, if that's possible) and only allowing access to call it in the code.

Please tell me if any of these is feasible and also give me a way to do it.

As said above, all you need is separate your code into a header and source file.Then, your compiler generates an object file when you compile; or a shared objects. Then you may ship the object files with the header file. The only downside of this is that you have to compile for each hardware architecture you are targeting. Here is one of the simplest way, which is sending object file with your header file.

//file foo.h
void * foo(int );

and implementation

//foo.c
#include "foo.h"
void * foo(int x)   
{
 //your code
};

now compile it with gcc

gcc -c foo.c -o foo.o

These generates foo.o. Now you can ship foo.h with foo.o or preferably a shared object made out of the object file.

Now your client could use your code getting only the header file and and .o file.

//file foouser.c 
 #include "foo.h"
 void * x = foo(2);

Assuming foo.o and foo.h are in the same directory,

gcc foouser.c -lfoo -o foouser

will generate the executable foouser.

There is a tutorial for the shared object version here and a good article about linkers and loaders here

You can compile your c++ code to a static or dynamic library

way to do it create a function defination in a header file

implement those function in .cpp file

compile it to either a static lib or dynamic dll with your header file aka the one who has the function declarations

On Linux http://www.iram.fr/~roche/code/c++/AddNumbers.html

On windows https://msdn.microsoft.com/en-us/library/ms235627.aspx

Compile And Give It In .a, .so ,.dll formats.

Before That Learn The Difference Between Them Difference between shared objects (.so), static libraries (.a), and DLL's (.so)?

If compiled remember it can be de-compiled.

The only way you can do this is probably to set up a web service that you host (or, more likely, a cloud service that you pay for).

Also remember that you are going to be maintaining this indefinitely, You might want to negotiate a maintenance fee.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM