saghul / pycares

Python interface for c-ares
https://pypi.org/project/pycares/
MIT License
165 stars 73 forks source link

[MSVC] setup_cares.py and '/MT' #124

Closed gvanem closed 3 years ago

gvanem commented 4 years ago

I did a py -3 setup.py -v build to try to build with MSVC-2019.

But the link failed miserably. I fail to understand how setuptools puts in a /MT in the CFLAGS for MSVC. And with the line in setup_cares.py: self.extensions[0].extra_link_args = ['/NODEFAULTLIB:libcmt'] the link is doomed to fail:

_cares.obj : error LNK2001: unresolved external symbol @__security_check_cookie@4
ares_gethostbyaddr.obj : error LNK2001: unresolved external symbol ___report_rangecheckfailure
ares_getnameinfo.obj : error LNK2001: unresolved external symbol _strchr
LINK : error LNK2001: unresolved external symbol __DllMainCRTStartup@12
build\lib.win32-3.6\pycares\_cares.cp36-win32.pyd : fatal error LNK1120: 13 unresolved externals

And these errors too (another issue with missing system-libs:

ares_gethostbyaddr.obj : error LNK2001: unresolved external symbol __imp__ExpandEnvironmentStringsA@12
ares_gethostbyaddr.obj : error LNK2001: unresolved external symbol __imp__GetLastError@0
ares_gethostbyaddr.obj : error LNK2001: unresolved external symbol __imp__GetWindowsDirectoryA@8
ares__timeval.obj : error LNK2001: unresolved external symbol __imp__GetTickCount@0
ares_init.obj : error LNK2001: unresolved external symbol __imp__GetVersionExA@4
ares_library_init.obj : error LNK2001: unresolved external symbol __imp__LoadLibraryW@4
ares_library_init.obj : error LNK2001: unresolved external symbol __imp__GetProcAddress@8
ares_library_init.obj : error LNK2001: unresolved external symbol __imp__FreeLibrary@4
ares_writev.obj : error LNK2001: unresolved external symbol __imp__SetLastError@4

Isn't /MT and /NODEFAULTLIB:libcmt a great contradiction?

Anyway, with this patch, it all worked:

--- a/setup_cares.py 2020-03-11 12:29:52
+++ b/setup_cares.py 2020-03-11 14:08:30
@@ -99,7 +99,8 @@
             self.compiler.add_include_dir(os.path.join(self.cares_dir, 'src/config_cygwin'))
         elif sys.platform == 'win32':
             if 'mingw' not in self.compiler.compiler_type:
-                self.extensions[0].extra_link_args = ['/NODEFAULTLIB:libcmt']
+                self.compiler.add_library('kernel32')
+            self.compiler.add_library('version')
             self.compiler.add_library('advapi32')
             self.compiler.add_library('iphlpapi')
             self.compiler.add_library('psapi')
saghul commented 4 years ago

What Python version was that? I don't see /MT when we build on AppVeyor: https://ci.appveyor.com/project/saghul/pycares/build/job/ymp3qxludl3sr8hp

gvanem commented 4 years ago

v 3.6, not too old. Besides, look at all the warnings in AppVeyor; adding a -DCARES_BUILDING_LIBRARY shoold solve that I think.

saghul commented 4 years ago

Besides, look at all the warnings in AppVeyor; adding a -DCARES_BUILDING_LIBRARY shoold solve that I think.

Possibly, but those are warnings, it builds and works ok. I have no idea where your /MT comes from...