From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Using Gnat.Sockets in a Windows DLL Date: Wed, 20 Apr 2016 19:10:50 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <220ee60f-3290-43d7-a097-cf90380d8bae@googlegroups.com> NNTP-Posting-Host: LMk7+sG0YqgPmReI4fVkAA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:30210 Date: 2016-04-20T19:10:50+02:00 List-Id: On 2016-01-13 21:25, ahlan.marriott@gmail.com wrote: > On Thursday, 26 November 2015 08:57:02 UTC+1, ah...@marriott.org wrote: >> We have windows programs and DLLs that use the Windows WinSock API directly. >> In order to make this code target independent (at least for Linux & OSX) we thought we should convert the code to use Gnat.Sockets. >> This is relatively trivial. >> Or at least it is for programs. >> When we try to convert our DLLs we run into a linker problem. >> If all we do is a "with Gnat.Sockets" then using GprBuild produces lots of messages saying that various entry points could not be found. >> See below. >> I recognize many of them as being in WS2_32.dll - which GprBuild finds when we build our programs (its in Windows/System32) >> Except that the names are not quite right so I suspect that this is the root cause of the problem. >> Has anyone any idea why a build of a DLL should behave differently to a build of a program with respect to name mangling? >> >> Best wishes, >> Ahlan >> ----------- >> >> gprbuild -d -PD:\Binary\Ada\tools\cal_dll\Cal_Dll.gpr -XWIN32ADA_BUILD=default -XLIBRARY_TYPE=static >> gprlib.exe Cal_Dll.lexch >> gnatbind -n -o b__Cal_Dll.adb -LCal_Dll -a -E D:\Binary\Ada\tools\cal_dll\objects\cal_dll_interface.ali >> gcc.exe -c -x ada -gnatA -gnatws b__cal_dll.adb -o b__cal_dll.o ... >> gcc.exe -shared -static-libgcc -o W:\product\windows\Cal_Dll.dll D:\Binary\Ada\tools\cal_dll\objects\cal_dll_interface.o ... >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socket.o):g-socket.adb:(.text+0x1b46): undefined reference to `gethostname@8' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socket.o):g-socket.adb:(.text+0x1c9c): undefined reference to `getpeername@12' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socket.o):g-socket.adb:(.text+0x2323): undefined reference to `getsockopt@20' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socket.o):g-socket.adb:(.text+0x3518): undefined reference to `getsockopt@20' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socthi.o):g-socthi.adb:(.text+0x1c): undefined reference to `WSASetLastError@4' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socthi.o):g-socthi.adb:(.text+0x246): undefined reference to `WSASetLastError@4' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(g-socthi.o):g-socthi.adb:(.text+0x27f): undefined reference to `WSASetLastError@4' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(socket.o):socket.c:(.text+0x109): undefined reference to `_imp__getservbyname@8' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(socket.o):socket.c:(.text+0x159): undefined reference to `_imp__getservbyport@8' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(socket.o):socket.c:(.text+0x2a4): undefined reference to `__WSAFDIsSet@8' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(socket.o):socket.c:(.text+0x34c): undefined reference to `_imp__ioctlsocket@12' >> C:\gnatpro\7.3.1\lib\gcc\i686-pc-mingw32\4.9.3\/adalib/libgnat.a(socket.o):socket.c:(.text+0x3b4): undefined reference to `_imp__WSAStringToAddressA@20' >> collect2.exe: error: ld returned 1 exit status >> gprlib: c:\gnatpro\7.3.1\bin\gcc execution error >> gprbuild: could not build library for project cal_dll >> [2015-11-26 08:36:34] process exited with status 4, 100% (36/36), elapsed time: 01.67s > > Dear all, > > It seems that this is an known problem. > At least to AdaCore. > They document it as KP-22-O331-001 > The problem is fixed in GprBuild v2.2.2 that is part of Gnat Pro 7.3.2 > Otherwise the workaround is not to produce an encapsulated DLL but > to produce a standard DLL and link in the static version of the Gnat > runtime using Library_options. > A standard DLL will link WS2_32 and linking in the static runtime will produce a standalone DLL. I built the latest grpbuild from Git sources (not easy under Windows). The problem with encapsulated library projects persists. Presently I believe it is related to the order in which libraries wsock32 and ws2_32 appear in the linker's command like. From what I read researching the issue, for some mysterious reasons, -lws2_32 -lwsock32 must appear *before* -shared -static-libgcc. So the linker line must look like: gcc.exe -lws2_32 -lwsock32 -shared -static-libgcc -o W:\product... There is "Leading_Library_Option" project attribute, but it is no help because it does not bring its content right after gcc.exe. Any ideas how to do it otherwise? For example, a very crude way would to write an application my_gcc_wrapper that calls gcc with -lws2_32 -lwsock32 preceding all other switches and replace package Linker is for Driver ("Ada") use "my_gcc_wrapper.exe"; end Linker; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de