2

I have a lib1.lib which has function fun1() which shows defined when i do nm. I have created my.so out of my.lib. But when i search my.so That fun1() is not there at all.since there might be no callers compiler might be ignoring it. So i created some dummy array like

char* dummyArray={
 &fun1;
};

in the file where fun1() is defined. But this also does not work. Kindly help.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
akris
  • 57
  • 1
  • 3
  • 11
  • In your example, `fun1` is a [pointer to a] char, not a pointer to a function. This identifier will have no collission with an identifier in a library. However, the compiler will complain about `fun1` being undefined, so please turn on compiler warnings. – Paul Ogilvie Dec 29 '16 at 09:25
  • when i do nm on lib1.lib i can see the symbole is defined but why is it when i generate .so out of it the symbol not there at all?? – akris Dec 29 '16 at 09:36
  • Because you do not use a _function_ called `fun1` (you only use a _char_ called `fun1`), the linker will not fetch the function it from the library. – Paul Ogilvie Dec 29 '16 at 09:41
  • will try the same with void* – akris Dec 29 '16 at 09:41
  • @PaulOgilvie The type will only be relevant in case of C++ due to name mangling (and I doubt that's the OPs problem). – yugr Dec 29 '16 at 13:22
  • Possible duplicate of [C++ differ between Linking with .o and with .a file: different behavior, why?](http://stackoverflow.com/questions/41364698/c-differ-between-linking-with-o-and-with-a-file-different-behavior-why) – jww Dec 29 '16 at 13:54
  • @yugr, I believe there will be a mentioning of an unresolved external `fun1` of type char in the object file. The linker will search for that, also in libraries, but it will not resolve it with (load) a _function_ `fun1`. – Paul Ogilvie Dec 29 '16 at 15:58
  • I just found that none of the symbols in .lib are there in .so,how ever when i am trying to create an .exe its there...Is it some thing wrong with .libs?will i need to recompile the .libs to be compatible to generate .so out of it?? – akris Dec 30 '16 at 09:38
  • Is there any compiler option to make compiler not to ignore functions that are not called?? – akris Jan 02 '17 at 10:25

2 Answers2

1

You need to force the compiler and linker to fetch the function from the library. In your current attempt, you reference a character called fun1 in your program. Because your fun1 is a char, the compiler has no reason to declare a symbol in your .o file that is a function called fun1. As a result, function fun1 will not be fetched from the library and there is no function fun1 in your executable.

(Note that in your example, there is no char fun1 declared, so your compiler should complain. Did you turn warnings on???)

To force the compiler and linker to load the function fun1 from the library, you can write:

char *fun1(void);    // prototype;

char *(*p)(void)     // p is a pointer to a function with same signature as fun1...
     = fun1;         // and we initialize it with function 'fun1'

Note: should the compiler determine that you never use p, it still may remove the function pointer variable and the assignment of fun1 to it, so fun1 would still not have been loaded.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Is there any compiler option to make compiler not to ignore functions that are not called?? – akris Jan 02 '17 at 10:26
  • The solution I gave you, if `p` is declared as a global or static variable, will ensure the function gets loaded. – Paul Ogilvie Jan 02 '17 at 15:05
1

Just wrap

-Wl,--whole-archive -lyourlib -Wl,--no-whole-archive

when linking .so. For background see c-differ-between-linking-with-o-and-with-a-file-different-behavior-why

Community
  • 1
  • 1
yugr
  • 19,769
  • 3
  • 51
  • 96