Is it possible to put the SDL .lib, .dll, and head

2019-09-06 11:56发布

问题:

My question is, is there a place(s) I can put the SDL .lib, dll, and header files, or process I can do, so when I create a C++ project I can just type #incldue <SDL2.h> instead of having to mess about with the project settings every time. Also making a project template is not a desirable solution to this question. Also, the IDE I am using is visual studio 2017 RC, running on windows 10. I also have access to the SDL source file so I could do something with the .c files if necessary.

Note that the desired solution would allow the programmer to include sdl in the same way they would include something like #include <stdio.h>, no extra work.

回答1:

Although "making a project template is not a desirable solution to this question", I suggest, regarding Visual Studio 2017 Community, make it. You can find instructions at (wikihow.com/Set-Up-SDL-with-Visual-Studio-2017). Following is an overview.

A. Downloading SDL and Creating Empty project.

A.1. Create folder SDL, and put folder SDL2-2.0.8 in it. In directory (disk) C:, create (by right click > New > Folder) new folder called SDL. If desired, you can choose another directory or folder, and another name for the new folder. Click at https://www.libsdl.org/download-2.0.php. Scroll to the bottom of the page and find Development Libraries. Under Windows: click the first entry: SDL2-devel-2.0.8-VC.zip (Visual C++ 32/64-bit). Drag (or, by right click, copy and paste) folder SDL2-2.0.8 from the window where it is downloaded, into folder C: > SDL.

A.2. Create Empty project. Open Visual Studio Community 2017, and create Empty project. In the Name: text box, type SDLproject. After the Location text box, click Browse... and navigate C: > SDL. Click Select folder. The Location now is C:\SDL. Make sure the Create directory for solution box is not checked. Click [OK]. Create source file with template C++ File (.cpp). Name it Source.cpp. The Location should be C:\SDL\SDLproject.

B. Installing SDL2-2.0.8 on Project

B.1. Add the Include folder. Right click on the name of your project, that is SDLproject (if you chose it), and select Properties. Open the C/C++ dropdown menu. Click General > Additional Include Directories > down arrow at the right of the field > Edit... in the drop down menu. At the top of the Additional Include Directories window, click the brown folder icon (the first icon). Click the three dots (...) next to the text box that is created. In the Select Directory window, navigate C: > SDL > SDL2-2.0.8, and click include folder. The Folder should be include. Click Select Folder on the Select Directory window and [OK] in the Additional Include Directories window.

B.2. Add the x86 folder. Open the Linker dropdown menu, and click General. Click Additional Library Directories entry, and click the down arrow at the right of the field. Click Edit... in the dropdown menu. At the top of the Additional Library Directories window, click the brown folder icon. Then click the three dots next to the text box that is created. In the Select Directory window, navigate C: > SDL > SDL2-2.0.8, and then double click lib folder, followed by clicking x86 folder. The Folder is x86. Click Select folder on Select Directory window and [OK] on Additional Library Directories window.

B.3. Add the two library files. In the Linker dropdown menu, click Input. Click the Additional Dependencies entry > the down arrow at the right of the field > Edit... in the dropdown menu. In Additional Dependencies window copy and paste SDL2.lib; SDL2main.lib in the top-most text box. Click[ [OK] in the Additional Dependencies window. In the Linker dropdown menu, click System > SubSystem. Click the down arrow and select Console(/SUBSYSTEM:CONSOLE) from the dropdown menu. Click [Apply], then [OK] on the Property Pages window.

B.4. Configure the linker subsystem to console. In the Linker dropdown menu, click System > SubSystem. Click the down arrow and select Console(/SUBSYSTEM:CONSOLE) from the dropdown menu. Click [Apply], then [OK] on the Property Pages window.

B.5. Copy the SDL2.dll file from x86 to your main project file. Navigate C: > SDL > SDL2-2.0.8 > lib > x86 in file explorer. In x86 folder click SDL2.dll file > right-click > Copy. Navigate to your C: > SDL > SDLproject. Right-click an empty area in SDLproject folder, and select Paste. The SDL2.dll file should now be in your project directory along with your Source.cpp file and a few other files created by Visual Studio.

Now you can compile and run your SDL program.

C. Creating project with SDL project template.

C.1. Create project template. By Windows's Paint program create icon with file name SDL. Go to Visual Studio and open SDLproject. On V.S. menu click Project > Export Template.... On Export template Wizard check Project Template..., if it's not checked. Click Next >. On Select Template Options, Template name:'s text field, type SDL. Click the upper [Browse...] (that one next to Icon Image: text field). Navigate to folder Images. Click image SDL. Click [Open]. Now path of Icon Image: is ending with \SDL.png. Click [Finish].

C.2. Create project with SDL project template.

C.2.1. On V.S. click File > New > Project.... On New Project window, click template: SDL. In Name:'s text field, type: SDLproject-1. Location is C:\SDL. Be sure Create directory for solution is unchecked. Click [OK].

C.2.2. On Solution Explorer, double click Source Files. Click Source.cpp > right click > click Exclude From Project.

C.2.3. Again right click Source Files > Add > New Item.... On Add New Item - SDLproject-1 window, click C++ File (.cpp). In Name:'s text field, type: Source-1.cpp. The Location is C:\SDL\SDLproject-1. Click Add. Now on Solution Explore, below Source Files, you have Source-1.cpp file only.

C.2.4. Navigate C: > SDL > SDLproject > click file SDL2.dll > right click > click Copy. Now navigate C: > SDL > SDLproject-1 > click on empty area > right click > click Paste. File SDL2.dll is in folder SDLproject-1 among Source-1.cpp and other 4 files.

C.2.5. Now you can compile your SDL project. On window This project is out of date:, below question Would you like to build it? click [Yes].

D. Remark: Creating project with SDL-project-template is as creating ordinary C++ project, but two more steps:

D.1. In Solution Explorer, Source Files, source file (Source.cpp in the example above) of template project (SDLproject, in the example) should be excluded from new project (SDLproject-1 in example).

D.2. File SDL2.dll should be copied from template project and pasted in new project.