简体   繁体   中英

“SDL2.DLL missing from computer” but is in the same folder as program?

SDL Folder in the same directory:

Contents of SDL folder:

I cant get my program to run as it says it cannot find SDL2.DLL? Any ideas?

Programs don't search your entire hard drive for DLLs. That would be expensive, horribly insecure, and bug prone.

Instead they have a search path, which depends on how you load it. Often your PATH environment variable is used, together with various other system places, as well as the directory of the process loading the dynamic library.

This list rarely includes "a subdirectory of the process executable called SDL ". You can, however, modify the search path; how exactly depends on how you are trying to load SDL.

The easy solution is to try to copy them all into the same directory.

You can use a Post-Build-Event to copy sdl2.dll into the directory where your .exe is built. Manually copying the file is fine for a quick test, but having Visual Studio do it for you has some advantages. For example, this lets you completely delete your Debug and Release directories and have the correct file copied when you do a clean build.

In your directory listing I see bin32 and bin64 directories inside the SDL directory. I assume that there is a copy of sdl2.dll directly inside each of those directories, but there are no separate debug and release versions, just 32-bit and 64-bit. Let me know if this is incorrect, otherwise, you can do this:

Open the Solution Explorer and select your project (not the solution). Then use Alt+Enter to go directly to the Property Pages. (Or right-click the project and select Properties at the bottom of the menu.)

In the selection panel on the left, go to Configuration Properties / Build Events / Post-Build Event .

At the top of the property sheet, set the Configuration dropdown to All Configurations and the Platform dropdown to All Platforms .

Click the edit box to the right of Command Line and enter this command:

copy "$(SolutionDir)SDL\bin$(PlatformArchitecture)\sdl2.dll" "$(TargetDir)"

Now click OK , and save and build your solution. You should find that it copies the correct version (32-bit or 64-bit) of sdl2.dll into the build directory.

The various $(FooBar) entries in the command line are macros which ar automatically expanded according to things like your project directory location, the type of platform you're building for, etc.

$(SolutionDir) is the directory containing your .sln file, with a trailing backlash .

$(PlatformArchitecture) is 32 or 64 depending on which platform you're building for.

$(TargetDir) is the directory that VS creates your .exe file in.

So this command line is an ordinary copy command as you might use in a command prompt, with the paths filled in to copy either SDL\\bin32\\sdl2.dll or SDL\\bin64\\sdl2.dll to your build target directory. We use quotes around the source and destination paths in case you have any spaces in your project path.

In some cases you may need specific commands for a particular configuration (Debug/Release) or platform (Win32/x64). You can do this by selecting that configuration or platform at the top of the property sheet. In our case, we can use one command for both 32-bit and 64-bit builds thanks to the $(PlatformArchitecture) macro.

To learn more about the macros, you can select the Command Line box in the property page and then click the drop-down arrow that appears to the right of the box, and select <Edit...> . This opens a multiline edit window (you can use more than one command in the build event), with an Evaluated value below it that shows the actual command that will be used for your current configuration and platform. You can inspect this command to see if it looks right, and you can also copy the evaluated command and paste it into a command prompt window for a quick test.

To see a full list of available macros and what they expand to for your current configuration/platform, click the Macros>> button below that and look through the list or use the search box at the top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM