With MSVC, the implementation of the 'getlogin' and 'getlogin_r' functions produces a link error:
/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../gltests -I.. -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o test-getlogin.obj `cygpath -w '../../gltests/test-getlogin.c'` test-getlogin.c /home/bruno/msvc/compile cl -nologo -MD -L/usr/local/msvc32/lib -o test-getlogin.exe test-getlogin.obj libtests.a ../gllib/libgnu.a libtests.a libgnu.a(getlogin.obj) : error LNK2019: unresolved external symbol __imp__GetUserNameA@8 referenced in function _getlogin test-getlogin.exe : fatal error LNK1120: 1 unresolved externals make[4]: *** [Makefile:6279: test-getlogin.exe] Error 2 /home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -I. -I../../gltests -I.. -DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I.. -I../../gltests/.. -I../gllib -I../../gltests/../gllib -D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o test-getlogin_r.obj `cygpath -w '../../gltests/test-getlogin_r.c'` test-getlogin_r.c /home/bruno/msvc/compile cl -nologo -MD -L/usr/local/msvc32/lib -o test-getlogin_r.exe test-getlogin_r.obj libtests.a ../gllib/libgnu.a libtests.a libgnu.a(getlogin_r.obj) : error LNK2019: unresolved external symbol __imp__GetUserNameA@8 referenced in function _getlogin_r test-getlogin_r.exe : fatal error LNK1120: 1 unresolved externals make[4]: *** [Makefile:6283: test-getlogin_r.exe] Error 2 The problem is that the use of GetUserName() requires linking with -ladvapi32, see https://msdn.microsoft.com/en-us/library/ms724432(v=vs.85).aspx . This fixes it. 2016-12-17 Bruno Haible <br...@clisp.org> getlogin, getlogin_r: Fix link errors on MSVC. * m4/getlogin.m4 (gl_LIB_GETLOGIN): New macro. * modules/getlogin (configure.ac): Require gl_LIB_GETLOGIN. (Link): New section. * modules/getlogin_r (Files): Add m4/getlogin.m4. (configure.ac): Require gl_LIB_GETLOGIN. (Link): New section. * NEWS: Mention the new link requirements. * modules/getlogin-tests (test_getlogin_LDADD): New variable. * modules/getlogin_r-tests (test_getlogin_r_LDADD): New variable. diff --git a/NEWS b/NEWS index fbbf6f2..3db6085 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,9 @@ User visible incompatible changes Date Modules Changes +2016-12-17 getlogin The link requirements of these modules are changed + getlogin_r from empty to $(LIB_GETLOGIN). + 2016-12-13 dfa Remove DFA_CASE_FOLD flag. Now based on RE_ICASE. 2016-11-17 unistr/u32-strmblen The function u32_strmblen can now return -1. diff --git a/m4/getlogin.m4 b/m4/getlogin.m4 index a03193b..a9dc4d3 100644 --- a/m4/getlogin.m4 +++ b/m4/getlogin.m4 @@ -1,4 +1,4 @@ -# getlogin.m4 serial 4 +# getlogin.m4 serial 5 dnl Copyright (C) 2010-2016 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -16,3 +16,17 @@ AC_DEFUN([gl_FUNC_GETLOGIN], HAVE_GETLOGIN=0 fi ]) + +dnl Determines the library needed by the implementation of the +dnl getlogin and getlogin_r functions. +AC_DEFUN([gl_LIB_GETLOGIN], +[ + AC_REQUIRE([AC_CANONICAL_HOST]) + case $host_os in + mingw*) + LIB_GETLOGIN='-ladvapi32' ;; + *) + LIB_GETLOGIN= ;; + esac + AC_SUBST([LIB_GETLOGIN]) +]) diff --git a/modules/getlogin b/modules/getlogin index 379052a..1348319 100644 --- a/modules/getlogin +++ b/modules/getlogin @@ -14,12 +14,16 @@ if test $HAVE_GETLOGIN = 0; then AC_LIBOBJ([getlogin]) fi gl_UNISTD_MODULE_INDICATOR([getlogin]) +AC_REQUIRE([gl_LIB_GETLOGIN]) Makefile.am: Include: <unistd.h> +Link: +$(LIB_GETLOGIN) + License: LGPLv2+ diff --git a/modules/getlogin-tests b/modules/getlogin-tests index 6facd60..c8cdb05 100644 --- a/modules/getlogin-tests +++ b/modules/getlogin-tests @@ -11,3 +11,4 @@ AC_CHECK_FUNCS_ONCE([ttyname]) Makefile.am: TESTS += test-getlogin check_PROGRAMS += test-getlogin +test_getlogin_LDADD = $(LDADD) $(LIB_GETLOGIN) diff --git a/modules/getlogin_r b/modules/getlogin_r index 5406d94..169cb44 100644 --- a/modules/getlogin_r +++ b/modules/getlogin_r @@ -4,6 +4,7 @@ getlogin_r() function: Get user name to a buffer allocated by the caller. Files: lib/getlogin_r.c m4/getlogin_r.m4 +m4/getlogin.m4 Depends-on: unistd @@ -17,12 +18,16 @@ if test $HAVE_GETLOGIN_R = 0 || test $REPLACE_GETLOGIN_R = 1; then gl_PREREQ_GETLOGIN_R fi gl_UNISTD_MODULE_INDICATOR([getlogin_r]) +AC_REQUIRE([gl_LIB_GETLOGIN]) Makefile.am: Include: <unistd.h> +Link: +$(LIB_GETLOGIN) + License: LGPLv2+ diff --git a/modules/getlogin_r-tests b/modules/getlogin_r-tests index eda7b45..868b1b6 100644 --- a/modules/getlogin_r-tests +++ b/modules/getlogin_r-tests @@ -11,3 +11,4 @@ AC_CHECK_FUNCS_ONCE([ttyname]) Makefile.am: TESTS += test-getlogin_r check_PROGRAMS += test-getlogin_r +test_getlogin_r_LDADD = $(LDADD) $(LIB_GETLOGIN)