简体   繁体   中英

Call function in shared library but not declaration in header files

I am doing some implementation about calling function of shared library via Java to JNI. And JNI is implemented by C code. Here is my question. There is a function named ABC() in shared library. This function is not be declared in header files. BUT with NDK build, it can be successful and this function also can work fine.

It's very confusing. Why? why can it be built by NDK and why can it be called without any exception? Thank you for answering:)

There are two possibilities here.

  1. The file using the function may contain its own declaration of the function. (Declarations are not required to be only in header files.)

  2. If you use a function without declaring it, the compiler may provide a default declaration. This is not part of modern C, but compilers may use old standards or be unduly lax in this regard. The default declaration is a function of the type int SomeName() . Such implicit declarations automatically have external linkage, which means the name will be matched to the definition of the function when the library is linked in.

If the latter is the case, you would be well advised to enable additional switches to tell the compiler to issue more warning messages and to use a modern language standard (such as C 2011), at least when developing new code.

Using a routine that is in a shared library but is not declared in its header is inadvisable. Such routines are often intended only for internal use of the library, and they may change or vanish in future versions of the library. So code using them would break; it would not be compatible with the new versions of the library.

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