问题
I'm stumped trying to link the SDL libraries against a new C++ Win32 Console application in MVS 2010.
I downloaded the latest SDL development libraries, and did the usual steps:
- Added the includes to the include path (pic) [This works fine, as MVS finds the #include files]
- Added the library path for SDL (pic) [This seems to work fine, as removing this additional path gives the error "error LNK1104: cannot open file 'SDL.lib'"]
- Added SDL.lib and SDLmain.lib to the linker additional dependencies (pic)
I edited the main file to have some test code, leaving it looking like this:
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include "SDL.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a;
SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO);
std::cout << "Pong" << std::endl;
std::cin >> a;
return 0;
}
And when I compile I get the following linker errors:
Error 1 error LNK2019: unresolved external symbol _SDL_Init referenced in function _wmain D:\All\Proyects\PFC\RT-Pong\RT-Pong\RT-Pong\RT-Pong.obj RT-Pong
Error 2 error LNK1120: 1 unresolved externals D:\All\Proyects\PFC\RT-Pong\RT-Pong\Debug\RT-Pong.exe 1 1 RT-Pong
In case I had a corrupted .lib, I downloaded the whole thing again, starting from 0, and got the same result. I'm honestly stumped, and it reeks of being a careless simple mistake that I'm overlooking. So if anyone has some fresh input on the matter, it would really make my day.
Thanks, Jaime
回答1:
I think you got linking error due to "wrong path defined at library path for SDL"
At Project->Properties->Configuration Properties->Linker->General->Additional Library Directories
Change the path from this: D:\All\Proyects\PFC\RT-Pong\SDL-1.2.15\lib\x64
Into this: D:\All\Proyects\PFC\RT-Pong\SDL-1.2.15\lib\x86
回答2:
Try to remove:
#include "stdafx.h"
#include <stdlib.h>
Switch:
int _tmain(int argc, _TCHAR* argv[])
to
int main(int argc, char* argv[])
Set in project properties to "console application"
Read this: http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet2010e/index.php
回答3:
make sure you are linking to the lib files and make sure the SDL.dll is in your system32 or sysWOW64(for x64 pc) or directory you are running the program from folder, if you are not sure they are linked you can use the following to link it and make sure your project is set to console.
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL.lib")
and always remember to properly close SDL and free resources when you are finished with it:
SDL_Quit();
return 0;
also when linking from your general include folder it is safer to use angled brackets.. ie:
#include <SDL/SDL.h>
here is a small example program ..so provided the .dll is in one of the places i listed above this should work.
来源:https://stackoverflow.com/questions/11482396/sdl-strange-linker-error-in-mvs