0

I'm porting what's primarily a very central (kind of like a libc extension) library from Linux to Cygwin and since I have a lot of setup and teardown and since I don't want to be using ctor/dtor attribute becaus of tcc compatibility, my library provides a main function and the user is supposed to be using a "my_main" function that gets called by the main function in the library.

It has worked very well on Linux but on Cygwin, I'm getting linker errors.

Here's an MCVE for building the library:

#!/bin/sh -eu
cat > libmain.c <<EOF
#include <stdio.h>
long my_main(int, char**);
int main(int C, char**V)
{

        int r = 0;
        puts("setup");
        if(0>my_main(C,V))
                r=-1;
        puts("teardown");
        return r;
}
EOF
gcc -shared -fpic -o libmain.so  libmain.c -Wl,--allow-shlib-undefined

The above works on Linux, but on Cygwin, I'm getting:

/tmp/cc0s6qei.o:libmain.c:(.text+0x32): undefined reference to `my_main'
/tmp/cc0s6qei.o:libmain.c:(.text+0x32): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `my_main'
collect2: error: ld returned 1 exit status

Can this be made to work on Cygwin?

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • Possible duplicate of [Building a shared library using gcc on Linux and MinGW on Windows](https://stackoverflow.com/questions/17601949/building-a-shared-library-using-gcc-on-linux-and-mingw-on-windows) – Ctx Aug 22 '18 at 14:45
  • Are you, by chance, compiling from 64-bit Linux to 32-bit Cygwin, or vice-versa? – Susmit Agrawal Aug 22 '18 at 15:01
  • @SusmitAgrawal Nope. My main sys is 64 bit Linux and the Cygwin I'm trying to build my project on is 64 bit Win7 with Cygwin. I have tried compiling it on 32 bit Linuxes but haven't tried 32 bit Cygwin yet. – Petr Skocik Aug 22 '18 at 15:05
  • Try prefixing `extern` before `my_main`. So the prototype would look like: `extern long my_main(int, char**);` – Susmit Agrawal Aug 22 '18 at 15:11
  • @SusmitAgrawal That doesn't and shouldn't do anything. Extern on non-static non-inline function declarations doesn't do anything – Petr Skocik Aug 22 '18 at 15:14
  • you can't leave `my_main` undefined at link time, it does not work on any Windows platform Cygwin included – matzeri Aug 22 '18 at 15:42
  • @matzeri I'm not very familiar with Windows. I'll read up on how its shared libraries work, but if you provide an executable recipe that fixes the example in the question before then, your answer might get upvoted and possibly accepted ;-). – Petr Skocik Aug 22 '18 at 15:45
  • Where is `my_main` defined ? – matzeri Aug 22 '18 at 15:47
  • @matzeri In the main program (instead of main) that links the library. – Petr Skocik Aug 22 '18 at 15:50
  • easy way is to move it in the library. – matzeri Aug 22 '18 at 16:14
  • @matzeri the whole point of this is that the user of library provides my_main. – Petr Skocik Aug 22 '18 at 16:16
  • see https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking for info on the matter of circular reference – matzeri Aug 23 '18 at 21:50

0 Answers0