Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2005: _sprintf already defined in ntdll.lib(ntdll.dll)

Tags:

c++

windows

I'm linking against ntdll.lib to use ZwQueryInformationProcess and am using the Multi-threaded runtime library.

ntdll seems to define some of the crt functions. So, when I link against it and also use the runtime library, I get linker errors.

Can I force the linker to use the crt functions and to ignore the ntdll symbols to get rid of the error? Or somehow else solve this error.

I know the documentation of ZwQueryInformationProcess suggests dynamic loading, but it's used in a static lib my main project is also linking to, and this has been working fine for a long time, so I'd prefer not to change it.

Thanks.

I've found a few discussions about this on other sites, eg: http://www.codeguru.com/forum/archive/index.php/t-414274.html but haven't seen anything that seems to provide a good solution. There are some mentions of changing the link order, but I can't see how to change the order in which the runtime library links via the project settings.

like image 276
Scott Langham Avatar asked Jan 24 '26 06:01

Scott Langham


1 Answers

Changing the link order seems to solve it. To change the order of automatically linked libs, you need to ignore them by adding to "Ignore Specific Library" and then re-add them to "Additional Dependencies" in the order you want them linked. The error message was this:

1>libcmt.lib(wcstol.obj) : error LNK2005: _wcstoul already defined in ntdll.lib(ntdll.dll)

So, I added libcmt.lib as ignored, and also placed it at the start of the additional dependencies. This produced another error, which I followed with the same steps. So, the project properties ended up looking like this (ntdll.lib is at the end of the Additional Dependencies):

Project properties screenshot

I also found adding /verbose:lib to the Linker -> Command Line -> Additional Options to be useful so that you can see exactly which libs are being linked and in what order.

like image 65
Scott Langham Avatar answered Jan 25 '26 21:01

Scott Langham