how do i call function of other's open source propriotery project in my project in C
Suppose you have a software written in C say XYZ. The software XYZ is an open source proprietary software. So I can have the source of the software. I can use the software but I can not modify XYZ's files.
Suppose I am writing my own software say ABC. And that software uses some of functionalities provided by XYZ.
Now there is function in source code of XYZ say "static int get_val( int index ) ". I want to use the function get_val(), so what should i do?
How should I call the开发者_开发问答 function??
You shouldn't. The static keyword makes the function local to it's translation unit (source file, more or less), which means it can not be called from other translation units.
Well of course you can, but it may not be a good idea.
There's two ways of making the function available:
- Export it from the module by removing the static keyword and adding it to the api header file. This will of course involve changing the original source.
- #include the file into your own source file, thus effectively making it part of your own translation unit. Depending on what other dependencies this file may have, this may or may not be a viable option. I would be very wary of doing this, but it is an option.
Build a shared library (DLL or .so) from XYZ. Chances are that there is a shared library already available. Link your code i.e. ABC with XYZ and you can start using the functions exposed by XYZ in your program. All open source software come with very good readme and instructions which will help you use the software. Start checking from those guides.
If the XYZ project is open source
- add the source files of the XYZ project to your own and compile all together
- if you change something in the XYZ project, consider sending a message to the maintainers of the project with your changes: they might like what you did and incorporate into a future version
If the project is proprietary, you don't have the source code. A function defined as static ...
is not visible in other translation units, so you don't call it at all.
精彩评论