Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access own custom global function of shared library in linux from other module in C

I have downloaded libgcrypt library source code and added my own customize function inside one particular file.

Although compilation/build process of customized shared library is successful, and both nm and objdump show the customized function is global, it nonetheless shows an error (undefined reference) at linking time.

Here is what I have done:

inside /src/visibility.c file, I have added my custom function,

void __attribute__((visibility("default"))) MyFunction(void)
{
    printf("This is added just for testing purpose");   
}

build process

./configure --prefix=/usr/local --disable-ld-version-script

sudo make install

nm and objdump command find this custom function as global inside shared library.

nm /usr/local/lib/libgcrypt.so | grep MyFunction
000000000000fbf0 T MyFunction


objdump -t /usr/local/lib/libgcrypt.so | grep MyFunction
000000000000fbf0 g     F .text  0000000000000013              MyFunction

Here is my sample code to access my custom function.

//gcrypt_example_test.c
#include <stdio.h>
#include <gcrypt.h>
#include <assert.h>

int main()
{
    MyFunction();
    return 0;
}


export LD_RUN_PATH=/usr/local/lib
gcc gcrypt_example_test.c -o test -lgcrypt

/tmp/ccA0qgAB.o: In function `main': gcrypt_example_test.c:(.text+0x3a2): undefined reference to `MyFunction' collect2: error: ld returned 1 exit status

Edit 1:

I tried all possible way to include function prototype declaration inside header file (/src/gcrypt.h) as follows:

   void __attribute__((visibility("default"))) MyFunction(void);   

... or:

    extern void __attribute__((visibility("default"))) MyFunction(void);  

... or:

    extern void   MyFunction(void);

... or:

    void   MyFunction(void);

I am still getting the same error (undefined reference) although no build error results in all above cases.

Why is this happening, and what mistake am I making?

Although other global functions which are part of standard shared library and defined inside visibility.c (nm also shows T for those functions) are accessible, why is my customized global function (MyFunction) of the shared library still inaccessible? Thanks!

Any link or explanation to resolve this error will be highly appreciable.

like image 972
bholanath Avatar asked Oct 21 '25 13:10

bholanath


1 Answers

From the GCC documentation (emphasis mine):

Some linkers allow you to specify the path to the library by setting LD_RUN_PATH in your environment when linking.

But, from the GNU ld man page:

   -rpath=dir
       Add a directory to the runtime library search path.  This is used
       when linking an ELF executable with shared objects.  All -rpath
       arguments are concatenated and passed to the runtime linker,
       which uses them to locate shared objects at runtime.  The -rpath
       option is also used when locating shared objects which are needed
       by shared objects explicitly included in the link; see the
       description of the -rpath-link option.  If -rpath is not used
       when linking an ELF executable, the contents of the environment
       variable "LD_RUN_PATH" will be used if it is defined.

Note that there is no mention at all of the link time library search path.

You need to compile/link with /usr/local/lib in the link time library search path:

gcc gcrypt_example_test.c -o test -L/usr/local/lib -lgcrypt
like image 82
Andrew Henle Avatar answered Oct 23 '25 04:10

Andrew Henle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!