Could someone explain what the
__imp__fprintf
and
__imp____iob_func
unresolved external means?
Because I get these errors when I'm trying to compile:
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol __imp__fprintf referenced in function _ShowError
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol __imp____iob_func referenced in function _ShowError
1>E:\Documents\Visual Studio 2015\Projects\SDL2_Test\Debug\SDL2_Test.exe : fatal error LNK1120: 2 unresolved externals
I can already say that the problem is not from linking wrong. I have linked everything up correctly, but for some reason it won't compile.
I'm trying to use SDL2.
I'm using Visual Studio 2015 as compiler.
I have linked to SDL2.lib and SDL2main.lib in Linker -> Input -> Additional Dependencies and I have made sure that the VC++ Directories are correct.
To link means not to work properly. Digging into stdio.h of VS2012 and VS2015 the following worked for me. Alas, you have to decide if it should work for one of { stdin, stdout, stderr }, never more than one.
As answered above, the right answer is to compile everything with VS2015, but for interest the following is my analysis of the problem.
This symbol does not appear to be defined in any static library provided by Microsoft as part of VS2015, which is rather peculiar since all others are. To discover why, we need to look at the declaration of that function and, more importantly, how it's used.
Here's a snippet from the Visual Studio 2008 headers:
So we can see that the job of the function is to return the start of an array of FILE objects (not handles, the "FILE *" is the handle, FILE is the underlying opaque data structure storing the important state goodies). The users of this function are the three macros stdin, stdout and stderr which are used for various fscanf, fprintf style calls.
Now let's take a look at how Visual Studio 2015 defines the same things:
So the approach has changed for the replacement function to now return the file handle rather than the address of the array of file objects, and the macros have changed to simply call the function passing in an identifying number.
So why can't they/we provide a compatible API? There are two key rules which Microsoft can't contravene in terms of their original implementation via __iob_func:
Any change in either of the above would mean existing compiled code linked against that would go badly wrong if that API is called.
Let's take a look at how FILE was/is defined.
First the VS2008 FILE definition:
And now the VS2015 FILE definition:
So there is the crux of it: the structure has changed shape. Existing compiled code referring to __iob_func relies upon the fact that the data returned is both an array that can be indexed and that in that array the elements are the same distance apart.
The possible solutions mentioned in the answers above along these lines would not work (if called) for a few reasons:
The FILE array _iob would be compiled with VS2015 and so it would be laid out as a block of structures containing a void*. Assuming 32-bit alignment, these elements would be 4 bytes apart. So _iob[0] is at offset 0, _iob[1] is at offset 4 and _iob[2] is at offset 8. The calling code will instead expect FILE to be much longer, aligned at 32 bytes on my system, and so it will take the address of the returned array and add 0 bytes to get to element zero (that one is okay), but for _iob[1] it will deduce that it needs to add 32 bytes and for _iob[2] it will deduce that it needs to add 64-bytes (because that's how it looked in the VS2008 headers). And indeed the disassembled code for VS2008 demonstrates this.
A secondary issue with the above solution is that it copies the content of the FILE structure (*stdin), not the FILE * handle. So any VS2008 code would be looking at a different underlying structure to VS2015. This might work if the structure only contained pointers, but that's a big risk. In any case the first issue renders this irrelevant.
The only hack I've been able to dream up is one in which __iob_func walks the call stack to work out which actual file handle they are looking for (based on the offset added to the returned address) and returns a computed value such that it gives the right answer. This is every bit as insane as it sounds, but the prototype for x86 only (not x64) is listed below for your amusement. It worked okay in my experiments, but your mileage may vary - not recommended for production use!
I managed to fix the problem.
The source of the error was this line of code, that can be found in the SDLmain source code.
So what I did was to edit the source code in SDLmain of that line too:
And then I built the SDLmain and copied and replaced the old SDLmain.lib in my SDL2 library directory with the newly built and edited.
Then when I ran my program with SDL2 no error messages came up and to code ran smoothly.
I don't know if this will bite me later, but so for everything is going great.
I have finally figured out why this is happening !
In visual studio 2015, stdin, stderr, stdout are defined as follow :
But previously, they were defined as:
So now __iob_func is not defined anymore which leads to a link error when using a .lib file compiled with previous versions of visual studio.
To solve the issue, you can try defining
__iob_func()
yourself which should return an array containing{*stdin,*stdout,*stderr}
.Regarding the other link errors about stdio functions (in my case it was
sprintf()
), you can add legacy_stdio_definitions.lib to your linker options.Use the pre-compiled SDL2main.lib and SDL.lib for your VS2015 project's library : https://buildbot.libsdl.org/sdl-builds/sdl-visualstudio/sdl-visualstudio-2225.zip
My advice is to not (try to) implement __iob_func.
While fixing these errors:
libpngd.v110.lib(pngrutil.obj) : error LNK2001: unresolved external symbol ___iob_func curllib.v110.lib(mprintf.obj) : error LNK2001: unresolved external symbol ___iob_func
I tried the other answers' solutions, but in the end, returning a
FILE*
C-array doesn't match up with an array of Windows' internal IOB structs. @Volker is right that it'll never work for more than one ofstdin
,stdout
orstderr
.If a library actually USES one of those streams, it will crash. As long as your program doesn't cause the lib to use them, you'll never know. For example,
png_default_error
writes tostderr
when the CRC doesn't match in the PNG's metadata. (Normally not a crash-worthy issue)Conclusion: It's not possible to mix VS2012 (Platform Toolset v110/v110_xp) and VS2015+ libraries, if they use stdin, stdout and/or stderr.
Solution: Recompile your libraries that have
__iob_func
unresolved symbols with your current version of VS and a matching Platform Toolset.