Error launching C++ executable in AIX

2019-07-20 15:43发布

问题:

I am facing a unique problem as part of our application migration from HP to AIX. The following simulated code produces different results in HP and AIX.

library.C **

    #include <stdio.h>
    #include "mylib.h"

    int libimgclientFNXXX()
    {
       int check = 100;
       check = FileNetDeleteDoc(check);
       return check;
    }

    int libimgclientFN()
    {
       int check = 1;
       printf("In lib ");
       return check;
    }

* main_func.C *

    #include <stdio.h>

    int libimgclientFN();
    int libimgclientFNXXX();

    int main()
    {
       int one = 0;
       if (1 == 1)
       {
            one = libimgclientFN();
       }
       printf("\n The status is %d \n", one);
    }

* mylib.h **

    extern int FileNetDeleteDoc (int);

Note that the function libimgclientFNXXX() is never called. My make file is as below:

xlC -c -g library.C -o library.o -I./
xlC -G -qmkshrobj  -o  libImgClient.so library.o
xlC -c -g  -qpic=small main_func.C -o main_func.o
xlC -brtl main_func.o -L. -lImgClient -o TST

When I run TST, I get the following loading error

$ TST
exec(): 0509-036 Cannot load program TST because of the following errors:
rtld: 0712-001 Symbol FileNetDeleteDoc__Fi was referenced
      from module ./libImgClient.so(), but a runtime definition
      of the symbol was not found.

Even though the function libimgclientFNXXX() is never called, there is unresolved errors.

The exact same code built in HP works fine with no errors.

Any inputs is appreciated.

Thanks,

回答1:

Yeah, "not using" a library function may still be an error, even if you are not going to call the code. It MAY defer the loading of some componanent until later, so it MAY not cause an error. Best not to have references to things that don't exist (or manually load the library and get the address, if the function doesn't exist, you'll get an error from the "find the function" call and you can do something sensible in the code).

The loader (the code that loads binary executables) isn't very clever, so it can't know exactly what is being called and what isn't. It's also possible that different compilers have different levels of cleverness for "dead code removal", so the one compiler completely removes the "never called" function, but another compiler doesn't remove it [because it's not got the cleverness to 100% certify that you never call the function - in gcc for example, it would know this if you made the libimgclientFNXXX a static function - because it knows that static functions don't get called outside this module, and this module isn't using it.



回答2:

AIX requires all symbols to resolve at load-time, so even though it builds OK, because the symbol is referenced the applications will not run.

You need to use lazy linking for the .so (the -blazy link option), which should cause the missing function to be linked only on first use.

You really should not be leaving undefined symbols in a library, though - if it needs symbols from another library, you should be linking to them (unless it's a plug-in, where the symbol is exposed in the app itself).



标签: c++ aix