问题
Hi: In Visual Studio 2012 Professional, Update 4, I can create a new OpenGL project pretty easily by creating a new Visual C++ project (using the blank template) and going into the NuGet Package Manager Console and typing:
Install-Package freeglut
Install-Package glew
Install-Package glm
To grab the libraries for freeglut, glew, and glm (a header-only math library.)
I can then create a simple example utilizing these libraries: (full example)
#include <GL/freeglut.h>
#include <GL/glew.h>
#include <glm/glm.hpp>
int main(int argc, char *argv[]) {
...
}
And then without any further configuration, I can hit the big green build button and everything compiles, links, and runs (finding the redistributable .dlls in the NuGet packages) and works fine.
In Visual Studio 2013, the same approach doesn't work: VS2013 complains that it cannot find freeglut.lib:
LINK : fatal error LNK1104: cannot open file 'freeglut.lib'
I can get the project to compile if I manually edit the Library path and copy the DLLs to the build output directory, but this seems severely less convenient.
Interestingly enough, even without setting or changing anything, Visual Studio seems smart enough to know to look for freeglut.lib, but it doesn't seem to know where to find it.
Is this a per-package difficulty, or did VS2013 change something about how Visual Studio handles NuGet packages?
回答1:
I have same problem, after Install-Package freeglut
Then I try to install another package: Install-Package nupengl.core
It works
回答2:
What solved the linkerror for me was going to properties->linker->input->additional dependencies and added opengl32.lib.
You also need to make sure that the freeglut.dll/glew32.dll/glfw3.dll are in the same folder as your executable. This is what install-package nupengl.core does for you.
回答3:
For static linking:
#define GLEW_STATIC
#define FREEGLUT_STATIC
#include <GL/glew.h>
#include <GL/freeglut.h>
Project->Properties->Configuration Properties->Referenced Packages->freeglut->Linkage (select static)
Do the same for Referenced Packages->glew
Now, in Linker->General->Additional Library Directories:
$(SolutionDir)\packages\freeglut.2.8.1.15\build\native\lib\v110\Win32\Debug\static;$(SolutionDir)\packages\glew.1.9.0.1\build\native\lib\v110\Win32\Debug\static
The library versions may change, and the path may change a little, but this is what works for me as of 14-MAY-2015.
EDIT: you still need to include "glew.lib" in Linker->Input->Additional Dependencies, so, bothering with the Referenced Package settings seems pointless (it works for freeglut, but not glew, and if you have to glew to the Additional Dependencies anyway, might as well just add freeglut, too).
For dynamic linking, in the Referenced Packages section, select "Dynamic Linking" for freeglut & glew, and either add or replace each package's library directory in the Additional Library Directories setting, and copy the DLL's from the redistributable directory to your Projects output directories, matching the debug DLL to the Debug directory, etc. Also, omit the XXX_STATIC #defines :)
The dynamic settings are a guess, but I know the static settings work.
来源:https://stackoverflow.com/questions/21569542/linking-against-nuget-libraries-in-visual-studio-2013