How to write API program [closed]
I like to know how to write a program which would export some API function.
For ex: Suppose I have few function, lets take add(int x,int y), mul(int x,int y) sub(int x, int y).
How can I write a daemon in c such that this will export above API to external world.
And how can I access these APIs from other program?
I am expecting from code point of view... Thanks in advance...
It depends on what you mean by API.
Creating a library
Create a header file. This will be the "interface" of your API. Programs that want to use your library will have to include it in order to do so.
libexample.h
int add(int x,int y);
int mul(int x,int y);
int sub(int x, int y);
Then you can go on and implement your library in your favorite system-specific way.
Creating a program that services requests
In this case, indeed you want a daemon and probably want to use RPC
. Using this scheme, external programs can call your functions via the network.
The easiest answer I can give you is Apache Thrift. You define a service interface (a bunch of function signatures), and set it up to listen on a port (the library does the networking calls for you). Then clients can connect and easily call your exposed methods. This works very well between languages.
An API (Application Programming Interface) is an abstract concept, it's the specification for to how users can call a provider of the API.
A library is a collection of object files which export some symbols (data and functions) and may have initializers and finalizers.
The API for a library consists of the exported symbols and the types of those symbols, which you would put on header files, so callers of your library can use that header files to access the symbols you provide.
The API for a RPC (Remote Procedure Call) service would be in the associated IDL files, which would be the equivalent of header files.
The API for a SOAP service would be in the associated WSDL files.
In linux you'll need to use *.so, in Windows - DLL. Anyway, you'll need to make a library -- static or dynamic. Take a look on shared libraries (Linux) or DLL's (Windows).
精彩评论