cygwin g++ Linker doesn't find shared library

血红的双手。 提交于 2019-11-30 13:06:31

After some experimenting I found a solution on how to compile a shared library under cygwin.

Apparently the compiler is looking for a DLL file even though it is inside cygwin. so the first step is to add your path, where the library is going to be to the PATH variable.

 export PATH=$PATH:/cygdrive/d/src/c/lib

Apparently when linking against a shared library, the linker seems to look for a DLL file by default. I don't know why, because inside cygwin I would expect it to look for a .so file just like on other UNIX systems.

However, there are two solutions to this, which both work.

First, you can create a link to your .so library with the name .dll

ln -s /cygdrive/d/src/lib/libsupport.so libsupport.dll

In this case the makefile doesn't have to be changed and -lsupport will find the library while linking. I prefer this solution.

Second, you can specify the linker option with the full name.

LIBS:=      -L /cygdrive/d/src/c/lib -l:libsupport.so

then you don't have to create a link.

So the crucial thing seems to be that the shared library must be in the PATH under cygwin. Using LD_LIBRARY_PATH doesn't help in that case as you can link the executable, but when trying to run it, it will not find it.

ldd nohupshd.exe

libsupport.so => not found

UPDATE: For some reason when I checked with ldd, my library was suddenly gone from the list. I found out that cygwin uses the name to differentiate between MS Windows and Unix shared libraries. So in order to make it work, the name of the library must be cyg.so to make it work, otherwise the exectuable seems to be some Windows build. In this case you don't need to create the link named x.dll as the shared library stays inside the Unix environment.

    $(LNK) -o cyg$(TARGET).so $(OBJ) -shared  

When using eclipse for debugging, the path to the shared library must also be in the windows path environment variable. Otherwise the debug session immediately terminates without an error.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!