MSBuild to apply a nuget package to a project at build time - c++

I have a cmake project that builds fine on Linux and that I want to build also on Windows. The code is portable and is made of C++ so... it should build. The only thing is that, on Windows, I need to use nuget to restore some packages, cpprestsdk is one of them. Now when I run
cmake .
CMake will generate solution files and projects files that I don't want to include in my VCS at all. The thing I want MSBuild to do is to apply the packages contained in my packages.config to all the project files found in the project directory.
Anyone knows how?

The thing I want MSBuild to do is to apply the packages contained in my packages.config to all the project files found in the project directory. Anyone knows how?
I am afraid you can not do such things. That because whether you are using MSBuild or NuGet to do this thing, you have to use to the nuget.exe to do it. However, install nuget packages to the project file relies on VS capabilities not nuget.exe and nuget team not plan to bring it to the exe.
The NuGet Command Line does not actually install any packages. The install command is actually doing a restore operation.
This means that nuget.exe install will only download and extract the package into the output location. It will not modify the project file. Although, the package manager console seem to offer "Install-Package" for doing this, but you have to open each project files with Visual Studio and execute Install-Package in the package manager console for each project files, which is not what you want. So you could not enable this script outside of visual studio.
So we could not use MSBuild to apply the packages contained in the packages.config to all the project files found in the project directory.
You can refer to this thread and this thread for details.
Hope this helps.

Related

Using Tesseact OCR in an existing Visual Studio C++ project

I want to use the Tesseract-OCR library with my already existing C++ project. I followed the steps mentioned in this answer.
I cloned the Github repo from here
Ran the build_tesseract.bat
Opened 'tesseract.sln' and set 'tesseract' as startup project before compiling it
for both 'release' and 'debug' configurations
After successful compilation, under the folder 'debug' and 'release', I have a list of .lib files
Now I don't know how to move forward from here. I am used to adding libraries that have 'include' directories. Here, I am not able to find such directories except the .lib files for both 'debug' and 'release' configuration.
Can someone point me to how to use these files to add the 'tesseract-OCR' API to my existing project?
For anyone looking to use Tesseract-OCR with Visual Studio 2017+, I found an alternative method(Not exactly, It was straight to my face all along).
Using VcPkg seems to be the best and easiest way as mentioned in Tesseract-OCR documentation itself.
Install VcPkg from the instruction here
Navigate to the installation folder or Add the VcPkg to the 'PATH' variable
Run the command vcpkg install tesseract:x64-windows. It might take 30 to 40 mins depending on your system
Under the 'Installed' folder, you will find 'include', 'lib' & 'bin' folders for all Visual Studio configurations
Using this, you can add Tesseract-OCR to your existing Visual Studio project

How to clean including nuget packages

If I want to keep backups of my code and I don't want all the libraries and nuget libraries in the backup, how do I do that?
I run clean on my solution and I noticed there are still DLL's and EXE in the bin and obj folders. Ok, I can delete those folders manually. I can delete the package folder which will remove all the nuget libraries, but if I ever want to restore, will my solution redownload all the nuget packages needed for the project? I'm afraid to test that theory and from what I read there is meta data and other things in the package folder will will cause me problems. If I zip what I have after deleting bin and obj folder, the zip is 450MB. If I go into the zip and delete the package folder, I see it is only 25MB. It seems to be all in the Package folder. From an ReactJS perspective, I can do a build and it cleans all this out and when I want to reimport for an existing project, I run a install. Is there not something like this for Visual Studio 2017? Will I be able to recover easily if I just delete the package folder before I back it up?
If I want to keep backups of my code and I don't want all the
libraries and nuget libraries in the backup, how do I do that?
Which way do you backup the code? According to your description, it seems you're trying to zip the solution and store it somewhere to do the backup. If so, you can delete the bin, obj and packages folders before you zip the solution. (You can even delete the .vs folder)
Is there not something like this for Visual Studio 2017? Will I be
able to recover easily if I just delete the package folder before I
back it up?
There're two ways to manage nuget packages in VS, packages.config and PackageReference format. Since you have packages folder in your solution, I think you use packages.config format. The info about nuget packages your project consume are defined in packages.config and xx.csproj file. After you delete the packages folder, you can easily recover the packages with the help of the packages.config file and xx.csproj file.
Import the solution which you've deleted bin, obj, packages folders in VS, right-click the solution in Solution Explorer and choose Restore nuget packages option, then it will help install the packages your solution need according to your packages.config file. So the answer is Yes for Will I be able to recover easily.
In addition:
1.When you install one nuget package, it will firstly install it in global-packages folder, and then copy them to your solution. So in same machine, if you zip the solution before delete the packages folder in solution. When you recover, it will first look for the cache, if the cache doesn't have the required nuget packages, it will install them from package source.(like nuget.org). You need to enable these two options in VS setting.
2.If you're using PackageReference format, the packages are stored in %userprofile%\.nuget\packages but not in your solution. And you can easily recover the packages though you zip the solution and copy it to remote server.
3.Apart from the restore nuget packages option in VS, you can also use command-line like nuget.exe, msbuild.exe... More details see here.
Why not just use some source control to do just that?
If you use Team Foundation Server with TFVC or Git it will do just that. You will have all your code under version control, without dll´s, bins, build artifacts, packages or anything. Without all those heavy, innecesary things that can be recreated by just rebuilding the solution again.
And of course you will enjoy all the advantages of having your code under source control: namely, not only having a backup of your code, but of all changes it has undergone with thus far.
Just saying.

How do I create nuget package and update metadata for C++/CLI vcxproj project

I have created C++/CLI project to wrap native library. How can I create nuget package using powershell command to add this wrapper library as well as native C++ dll it wraps.
I can see some posts for how to use nuget packages in C++/CLI projects but cannot find anything useful for actually creating nuget package for C++/CLI project itself. Nuget.exe "pack" command fails for .vcxproj project.
The NuGet.exe will automatically replace metadata like id, version etc from assembly/csproj file but we are not using vcxproj to build nuget package for C++ CLI project.
I solved the problem by using nuspec file directly while building the library from bamboo itself. I used bamboo to set the build version of the metadata. All other metadata is hardcoded in the nuspec file which does not need to change with every build.
& "NuGet.exe" pack "Library.nuspec" -Version ${bamboo.BuildVersion} -OutputDirectory ${bamboo.build.working.directory}

Where are nuget package manager include directories set in VS 2015?

i am currently trying my first steps with nuget package manager (3.4.3.855) in VS 2015 (Enterprise). For my C++/Qt project i managed to find some libraries i need and they seem to be correctly downloaded to <myprojectDir>/packages/<package_name>/... folders.
However, it is a mystery to me, where and how include folders for headers and/or libs are specified for the project?
I mean, for example with the Eigen library, after importing the nuget package i can just #include <Eigen/core>, which is located in
<myprojectDir>\packages\Eigen.3.2.9\build\native\include
and everything works fine, but i neither have the packages\Eigen.3.2.9\build\native\include-path popping up in the VC++ Directories->Include Directories list in the property pages nor is there a custom property page in the project properties (yes, i did not click the solution properties ;) ) as seen e.g. in this example video with the zlib package. I do not even have this property page if I install the zlib package.
While it seems to work somehow under the hood for Eigen, it does not for the Visual Leak Detector (vld) package... so knowing how things work would be great ;)
Thanks for any help...
From NuGet 2.5, C++ project recognizes the installed packages through MSBuild properties and targets files from NuGet package. After installing packages in C++ project, the MSBuild files are imported into your project file. So the projects will know how to find and use the contents of the NuGet Packages.
To make MSBuild integration better, NuGet has created a new convention for automatically importing MSBuild properties and targets from a NuGet package. Alongside the existing \content, \lib, and \tools folders, NuGet now recognizes a new top-level folder: \build. You could open the Eigen package that you have installed through NuGet Package Explorer, there has a \build folder and a Eigen.targets file which contains MSBuild properties.
Please refer to the MSBuild Integration part from below link:
http://blog.nuget.org/20130426/native-support.html

Building/Running Google Test

I am trying to build and run Google Test but encountered some issues.
Any help greatly appreciated.
I used CMake 2.8 (with UI) to create .sln and .vcproj files in the D:/MyBuild
directory (as mentioned in the README file of google test).
However, when I try to run the .sln file in the MyBuild directory,
here is what I get (image):
Also, here is a screenshot of the CMake which I used to
create .sln files and .vcproj files in the D:/MyBuild directory.
ps. One thing I noticed is that README was giving instructions how
to use non graphical version of CMake (e.g., 2.6) to install
google test, however I used one with user interface - maybe
I did something wrong there?
You appear to have successfully built gtest's libs in Debug mode. However, by default the .sln doesn't contain any executables, so you can't actually run anything.
If you select the checkboxes in the CMake GUI for gtest_build_samples and/or gtest_build_tests and hit "Generate", then the Visual Studio solution should contain some executables.
From memory, these don't build cleanly, but I don't have MSVC 2010 to hand, so I can't be sure.