www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Linker can't link functions from advapi

reply Deen O'Connor <deen.oconnor yandex.ru> writes:
So i got this problem when i tried to compile my project in VS 17 
with VisualD plugin.

It compiled just fine, i got no errors from DMD, but both optlink 
and vs linker tell me that i have 3 unknown functions - 
OpenProcessToken, LookupPrivilegeValue and AdjustTokenPrivileges.

I tried converting advapi32.dll to .lib, this did not solve the 
problem, because with the necessary .lib file set in project 
settings i get a message telling me that my advapi32.lib is 
corrupt.

Idk what to do, and i don't want to re-write everything with 
another language.
Apr 01 2019
next sibling parent Kagamin <spam here.lot> writes:
Maybe you didn't setup the project to link with advapi32.lib
Apr 01 2019
prev sibling next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 1 April 2019 at 10:07:58 UTC, Deen O'Connor wrote:
 So i got this problem when i tried to compile my project in VS 
 17 with VisualD plugin.

 It compiled just fine, i got no errors from DMD, but both 
 optlink and vs linker tell me that i have 3 unknown functions - 
 OpenProcessToken, LookupPrivilegeValue and 
 AdjustTokenPrivileges.

 I tried converting advapi32.dll to .lib, this did not solve the 
 problem, because with the necessary .lib file set in project 
 settings i get a message telling me that my advapi32.lib is 
 corrupt.

 Idk what to do, and i don't want to re-write everything with 
 another language.
You shouldn't need to convert it for the MS linker. The proper 32- and 64-bit COFF format libs should have been installed with VS 17.
Apr 01 2019
parent reply Deen O'Connor <deen.oconnor yandex.ru> writes:
On Monday, 1 April 2019 at 14:04:35 UTC, Mike Parker wrote:
 You shouldn't need to convert it for the MS linker. The proper 
 32- and 64-bit COFF format libs should have been installed with 
 VS 17.
I know, but if i don't set the .lib file for the linker, it tells me it doesn't know those functions.
Apr 01 2019
next sibling parent reply kinke <noone nowhere.com> writes:
On Monday, 1 April 2019 at 15:10:20 UTC, Deen O'Connor wrote:
  I know, but if i don't set the .lib file for the linker, it 
 tells me it doesn't know those functions.
So the only issue is that the lib isn't referenced automatically and that you have to specify it explicitly? Then all that's missing is a `pragma(lib, "advapi32")` in the module referencing these functions; is that druntime?
Apr 01 2019
parent Deen O'Connor <deen.oconnor yandex.ru> writes:
On Monday, 1 April 2019 at 19:08:01 UTC, kinke wrote:
 Then all that's missing is a `pragma(lib, "advapi32")` in the 
 module referencing these functions; is that druntime?
It worked, thanks a lot.
Apr 02 2019
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 1 April 2019 at 15:10:20 UTC, Deen O'Connor wrote:
 On Monday, 1 April 2019 at 14:04:35 UTC, Mike Parker wrote:
 You shouldn't need to convert it for the MS linker. The proper 
 32- and 64-bit COFF format libs should have been installed 
 with VS 17.
I know, but if i don't set the .lib file for the linker, it tells me it doesn't know those functions.
Right. That’s how the compile-link model works. While it would be possible to add a lib pragma to every system module in DRuntime so that the appropriate library is automatically linked when the module is imported, that’s not currently the case. So you do have to provide them to the linker. When using Optlink, some commonly system libs are automatically linked, like kernel32 for example, but not when using the MS linker.
Apr 01 2019
next sibling parent reply kinke <noone nowhere.com> writes:
On Monday, 1 April 2019 at 21:10:20 UTC, Mike Parker wrote:
 While it would be possible to add a lib pragma to every system 
 module in DRuntime so that the appropriate library is 
 automatically linked when the module is imported, that’s not 
 currently the case.
I count 62 `pragma(lib, ...)` in druntime, incl. advapi32 [1]. Missing ones can be seen as a bug, or at the very least an unnecessary inconvenience. [1] https://github.com/dlang/druntime/blob/aa39818f2805d7406d06c9fc74b7903789ca13ac/src/core/sys/windows/aclapi.d#L14
Apr 01 2019
parent reply destructionator gmail.com writes:
On Mon, Apr 01, 2019 at 09:46:51PM +0000, kinke via Digitalmars-d wrote:
 I count 62 `pragma(lib, ...)` in druntime, incl. advapi32 [1]. Missing ones
 can be seen as a bug, or at the very least an unnecessary inconvenience.
So yes and no. I agree they should be there, but they are unlikely to actually work because the druntime modules are not in the active build and thus their pragmas don't apply.
Apr 01 2019
parent Mike Parker <aldacron gmail.com> writes:
On Tuesday, 2 April 2019 at 01:18:54 UTC, destructionator wrote:
 On Mon, Apr 01, 2019 at 09:46:51PM +0000, kinke via 
 Digitalmars-d wrote:
 I count 62 `pragma(lib, ...)` in druntime, incl. advapi32 [1]. 
 Missing ones can be seen as a bug, or at the very least an 
 unnecessary inconvenience.
So yes and no. I agree they should be there, but they are unlikely to actually work because the druntime modules are not in the active build and thus their pragmas don't apply.
Yeah, I was surprised to learn that they are there. I've had to link with certain system libs on Windows as recently as a few months ago and had simply assumed the pragmas weren't there.
Apr 01 2019
prev sibling parent Deen O'Connor <deen.oconnor yandex.ru> writes:
On Monday, 1 April 2019 at 21:10:20 UTC, Mike Parker wrote:

 Right. That’s how the compile-link model works. While it would 
 be possible to add a lib pragma to every system module in 
 DRuntime so that the appropriate library is automatically 
 linked when the module is imported, that’s not currently the 
 case. So you do have to provide them to the linker.

 When using Optlink, some commonly system libs are automatically 
 linked, like kernel32 for example, but not when using the MS 
 linker.
I see, thank you for explaining that.
Apr 02 2019
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Monday, 1 April 2019 at 10:07:58 UTC, Deen O'Connor wrote:
 So i got this problem when i tried to compile my project in VS 
 17 with VisualD plugin.

 It compiled just fine, i got no errors from DMD, but both 
 optlink and vs linker tell me that i have 3 unknown functions - 
 OpenProcessToken, LookupPrivilegeValue and 
 AdjustTokenPrivileges.

 I tried converting advapi32.dll to .lib, this did not solve the 
 problem, because with the necessary .lib file set in project 
 settings i get a message telling me that my advapi32.lib is 
 corrupt.

 Idk what to do, and i don't want to re-write everything with 
 another language.
I would advise against using optlink, make sure your object file format is COFF. I can recommend setting up your project for dub[1]. All I had to do is add this to dub.json: "libs": [ "Gdi32", "Comdlg32", "Winspool", "Ws2_32", "wntab32x", "User32" ], and compile with `dub build -a=x86_mscoff` for 32bit or `dub build -a=x86_64` for 64bit. It just works. Granted I also have this in dub.json "lflags-x86_mscoff": ["/NODEFAULTLIB:LIBC.lib"] but I believe that is necessary because of some other libs that we link with. [1] https://dub.pm/getting_started Bastiaan.
Apr 01 2019
next sibling parent Deen O'Connor <deen.oconnor yandex.ru> writes:
On Monday, 1 April 2019 at 15:33:46 UTC, Bastiaan Veelo wrote:
 I would advise against using optlink, make sure your object 
 file format is COFF. I can recommend setting up your project 
 for dub[1]. All I had to do is add this to dub.json:

  	"libs": [
 		"Gdi32",
 		"Comdlg32",
 		"Winspool",
 		"Ws2_32",
 		"wntab32x",
 		"User32"
 		],

 and compile with `dub build -a=x86_mscoff` for 32bit or `dub 
 build -a=x86_64` for 64bit. It just works. Granted I also have 
 this in dub.json

   	"lflags-x86_mscoff": ["/NODEFAULTLIB:LIBC.lib"]

 but I believe that is necessary because of some other libs that 
 we link with.
That didn't work. Linking failed with code 1120 with the same errors as before.
Apr 01 2019
prev sibling parent reply Deen O'Connor <deen.oconnor yandex.ru> writes:
On Monday, 1 April 2019 at 15:33:46 UTC, Bastiaan Veelo wrote:
 I would advise against using optlink, make sure your object 
 file format is COFF. I can recommend setting up your project 
 for dub[1]. All I had to do is add this to dub.json:

  	"libs": [
 		"Gdi32",
 		"Comdlg32",
 		"Winspool",
 		"Ws2_32",
 		"wntab32x",
 		"User32"
 		],

 and compile with `dub build -a=x86_mscoff` for 32bit or `dub 
 build -a=x86_64` for 64bit. It just works. Granted I also have 
 this in dub.json

   	"lflags-x86_mscoff": ["/NODEFAULTLIB:LIBC.lib"]

 but I believe that is necessary because of some other libs that 
 we link with.
Linker failed with exit code 1120, same errors as before.
Apr 01 2019
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 1 April 2019 at 19:09:50 UTC, Deen O'Connor wrote:
 On Monday, 1 April 2019 at 15:33:46 UTC, Bastiaan Veelo wrote:
 I would advise against using optlink, make sure your object 
 file format is COFF. I can recommend setting up your project 
 for dub[1]. All I had to do is add this to dub.json:

  	"libs": [
 		"Gdi32",
 		"Comdlg32",
 		"Winspool",
 		"Ws2_32",
 		"wntab32x",
 		"User32"
 		],
Linker failed with exit code 1120, same errors as before.
You don't need all of those if you aren't using them, but did you add advapi32 to the list?
Apr 01 2019