Adding projects to a visual studio c++ solution via a script - c++

I usually work on multi-project solutions in visual studio.
Since the solutions themselves are not stored in the repository, I spend some time adding in the various projects via visual studio(from a list which is part of the 'parent' project).
I am wondering if I can acommplish this via a script.
ie: 1. create a solution. 2 add projects to that solution.
I have a supplementary I need to add to the above question.
I can use the File.AddProject from within the command window of visual studio(assuming you have a project open).
I can also use File.OpenExistingProject from outside of visual studio using devenv /command ..
Now the only missing piece is how do I add exisitng project to the project(solution) that is open, from outside of visual studio.

You should give a try to CMake. Here is a CMakeLists.txt example that creates a library and two binaries using that library. After using CMake program, you end up with a solution and 3 projects inside.
project(MyProject)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/common)
file(GLOB_RECURSE common_files common/*.h common/*.cpp)
add_library(commonLibrary ${common_files})
file(GLOB_RECURSE projectA_files projA/*.h projA/*.cpp)
add_executable(ProgramA ${projectA_files})
target_link_libraries(ProgramA commonLibrary)
file(GLOB_RECURSE projectB_files projB/*.h projB/*.cpp)
add_executable(ProgramB ${projectB_files})
target_link_libraries(ProgramB commonLibrary)

I have to admit , I did'nt try Cmake , mostly because I thought what I was looking for should be exposed in visual studio itself. I have the elements of the solution.
windows command prompt to add an alias for AddExistingProject followed by a command prompt run of devenv /commandline. It should be possible to write a script that utilizes these elements.

Related

Visual Studio Cross-Platform CMake - Includes aren't picked up, intellisense is upset about it

I have a C++ project set up something like this:
Linux box:
/home/me/.vs/SomeBigProjectDir
/opt/DependencyOne
/opt/DependencyTwo
Windows:
Local copy of the source
I've set up the cross-platform connection in Visual Studio, and am able to trigger builds on the remote host no problem. However, Intellisense gets upset about #includes - it claims that it can't find any of the included files specified.
Visual Studio has been told to invoke CMake with -DDependencyOne=/opt/DependencyOne and -DDependencyTwo=/opt/DependencyTwo. It's a bit yucky, but we're halfway through migrating off of autotools (these were previously in a configure script), and I don't see how they'd interfere with this. Having these more cleanly packed into some CMake file is an upcoming task.
Sample CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(3.14)
PROJECT(MyProject)
include_directories(${CMAKE_SOURCE_DIR})
if (DependencyOne)
include_directories(${DependencyOne}/include)
endif()
if (DependencyTwo)
include_directories(${DependencyTwo}/include)
endif()
#blah
add_subdirectory(SubDirOne)
add_subdirectory(SubDirTwo)
#etc
#various targets are declared in subdirectories
The project itself contains many targets, most of which has a dependency on DependencyOne.
If I run a build, Visual Studio happily rsyncs across any file changes, triggers the build on the remote, and it completes without issue. In addition, it has copied back all of the includes to local (on Windows), where they live in \AppData\Local\Microsoft\Linux\HeaderCache\1.0<some numbers>\opt\DependencyOne, for example. So, it clearly knows about them as includes.
Intellisense does not seem to want to believe that these files exist, and I can not find where to tell it to look to find them. Having red squiggles on tons of code that uses these dependencies is just unhelpful, and there is no way to F12 into the header file.
I've tried adding the remote include directories to the remoteCopyAdditionalIncludeDirectories within CMakeSettings.json on Visual Studio to no avail.
I've also tried looking around the internet, but Microsoft helpfully called Visual Studio and Visual Studio Code very similar names, and it's tough to find results specifically for Visual Studio that relate to this type of problem.
Other maybe-relevant information:
Visual Studio 2019 16.9.2
CMake 3.14.6
Red Hat Enterprise Linux 7
Edit:
I re-read through this and thought I could add a little more detail.
To my understanding, in CMake, using include_directories will result in compilers being happy with the following:
include_directories(/opt/dependency/include)
for:
opt
-dependency
--include
---Dep
----SomeHeader.h
----AnotherHeader.h
#include <Dep/SomeHeader.h> //works anywhere that the include_directories() statement applied
In this case, there is that above-mentioned directory living in AppData. The root of the issue is that intellisense doesn't have the sense to look in that directory, despite the project "knowing" that includes are there (it copied the headers over). Because this is a CMake project, there is no CppProperties.json to edit to manually add the paths, at least, not one that I have found.
Edit:
This appears to be a repeat of a bug reported here: https://developercommunity.visualstudio.com/t/Intellisense-in-CMake-setup-does-not-rea/957818. I was quick to judge all headers as causing intellisense to fail - it is only those that have a '.' character in their path. I have reported this to Microsoft.

Visual Studio 2017 - Disable CMake for dependencies

I have a Visual Studio 2017 Solution that has some header-only dependencies that are multiplatform and use CMake.
I do not need CMake at all, but I keep getting a lot of warnings and issues because Visual Studio 2017 insists on using CMake, exploring these projects, etc.
Is there a way to completely disable the CMake feature for this solution/project?
Update:
I load the project as a solution not as a CMake project.
It is a header-only library and it does not require CMake to work. It is just using it for testing and other purposes.
I keep the dependency as a git submodule so I don't want to make changes to it. It is correctly working by adding it to the main project as an include path, etc.
The only thing I want I want is to avoid is CMake autodiscovery in these directories because I have no use for it.
For instance:
Add Eigen to your project as a submodule in a directory called Eigen. (This is a header-only library)
Write some code in your solution and reference the header files (you need to add the correct include paths, etc.)
You will see that CMake will pick Eigen (when it is not actually necessary)
You will get warnings and Targets, etc.
Don't open the CMakeLists.txt with File->Open->CMake. Choose File->Open->File. You can also try to uninstall the visual studio c++ tools for cmake portion of Visual Studio 2017 via the installation tool.
In Solution Explorer, locate CMakeLists.txt, right-click it.
Select Custom Build Tool->General.
Erase everything from there. Make sure you have a backup just in case ...

Compiling Qt projects in Qt/MsBuild format without Qt VS Tools installed

I have many Qt projects in Visual Studio, using the new Qt/MsBuild format provided by the Qt VS Tools. When compiling in my development environment, where I have the Qt VS Tools installed, everything works flawlessly (compiling from IDE and from command line).
We have a computer dedicated to nightly builds, where only the compiler and msbuild are available (no IDE nor Qt VS Tools are installed).
When compiling the projects in such computer we get an error:
QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly.
Followed by several lines such as
e:********\Preferences.h(4): fatal error C1083: Cannot open include file: 'ui_Preferences.h': No such file or directory
(Project contains Preferences.ui).
How can I solve such errors when Qt VS Tools are not installed?
One solution provided by the Tools' creators is to copy %LOCALAPPDATA%\QtMsBuild into each project directory. But we are talking about hundred of projects. Doing manually, and more on, pushing them as part of the project itself doesn't sound very elegant.
One option would be to add a pre-build step that copies it from a common place into each project (and adding a **/QtMsBuild line to each .gitignore file). Again, looks like too much work.
When looking at the .vcxproj file for the Qt project you find this fragment (the reason for the solution provided by creators):
<PropertyGroup Condition="'$(QtMsBuild)'=='' or !Exists('$(QtMsBuild)\qt.targets')">
<QtMsBuild>$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup>
So, the simplest solution (without being able to install the tools), is to copy the %LOCALAPPDATA\QtMsBuild directory (from a system with the Tools installed) into the night computer (in any common place, but I decided to keep the location used by the tools) and then setting an environment variable:
set QtMsBuild=%LOCALAPPDATA%\QtMsBuild
PS: do not add double quotes to the variable (at least I had problems with them, so VS couldn't find the files).
Update 9-14-2020
I'm not sure on which version it started, but Qt projects created with (at least) the v2.5.2 Qt VS Tools fails to compile indicating that the Qt version has not been set. To solve so, you can
Copy the Registry entries from a computer with tools installed, located at HKCU\SOFTWARE\Digia\Versions.
If you will rely on a single Qt version (but that may be update globally for all projects), you can skip the Registry and just set the Qt version of all projects to $(DefaultQtVersion) (the same used in past project formats) and define an environment variable pointing to the directory of the version: set DefaultQtVersion=c:\Qt\Qt_5_15_0\Win32, for example.
I had problems in my case setting up $Env:QtToolsPath="$Env:QT_PATH\bin" fixed the problem. (It was not finding qmake for some checks). This, I think, is required when using QtMsBuild v3.3
Not sure if this info is 100% correct but may help someone.
For me to use MsBuild with Qt using QtMsBuild (not installing Visual Studio and/or VS plugins), I need to set up:
Example (PowerShell):
- $Env:PATH="$Env:MSBUILD_PATH;$Env:PATH"
- $Env:QT_PATH="D:\BuildTools\Qt\5.15.1\msvc2019_64"
- $Env:QtMsBuild="D:\BuildTools\Qt\QtMsBuild303" #303 Against visual studio project version
- $Env:QtToolsPath="$Env:QT_PATH\bin" #Support QtMSBuild , At desktop is done by the plug in
- $Env:PATH="$Env:QtMsBuild;$Env:PATH"
- $Env:PATH="$Env:QT_PATH\bin;$Env:PATH"
I hit this and got it working.
I followed #cbuchart 's advice; but needed to do a little more/different.
So here is what I did. I copied %LOCALAPPDATA%\QtMsBuild to my repo. I then edited my .vcxproj with a text editor. I adjusted the line that looks like:
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
To be:
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\..\QtMsBuild</QtMsBuild>
Depending on the relative location of where your project(s) are to the copied QtMsBuild directory you might want to adjust this. In my case the .vcxproj was in a directory one level from the directory that contains the QtMsBuild directory I copied.
Next in the QtMsBuild/Qt.props file I added the following:
<PropertyGroup>
<DefaultQtVersion>$(MSBuildThisFileDirectory)\..\Qt5.15.0\msvc2019_64</DefaultQtVersion>
<QtToolsPath>$(DefaultQtVersion)\bin</QtToolsPath>
</PropertyGroup>
That was added right after the opening Project tag in that file.
This was as #cbuchart mentioned something might have changed. I didn't do the Registry idea; but added those lines instead. I think it is possible to add them to a different file that gets imported before Qt.props if desired.
Now as for what this "Qt5.15.0\msvc2019_64" is; you'll need the tools, includes, libraries from a computer with Qt installed in order to build (also DLLs if your build needs to run the executable (i.e. tests)). This might be a lot of files so you can reduce it some if you know what you are doing. It might make sense to have these be put into it's own submodule or something.
After that it should uic/moc files and ultimately build (and run).
I got too. Maybe you need update your visual studio and then restart your computer ...
problem solved. reason: install 'qt vs tools' latest version but the visual studio
not support of it.

What to do next after building wxWidgets successfully : Visual Studio 2010

I have downloaded c++ open source application which was created using under Linux IDE/Editor (or maybe something NOT Windows) and its no longer supported.
This time I want to edit and recompile it using VS 2010.
Error(s) left (mostly prefixed with "wx") such as:
"wxString is undefined"
"wxStaticText expected a type specifier"
etc... makes me think that this is all about wxWidgets.
After successfully building wxWidgets, what to do next to integrate it to the actual c++ program for me to be able to edit and compile using VS 2010?
I use BorlandC++ last 2 years ago but not familiar with GUI packages.
The first step is to tell the compiler where to look for the wxWidgets include files.
So, you need to find out where these files are, which depends on where you installed and built wxWidgets.
On my system, this is C:\Program Files (x86)\wxWidgets-2.8.12
I consider the best way is to, instead of hard-coding the path in every project, create an environment variable WXROOT to store the path to your wxWidgets folder.
Control Panel | System | Advanced | Environment variables
Now you have to tell the compiler about this
Visual Studio | Project | Properties | C/C++ | Additional Include Directories
What you add here depends on what kind of build you are doing. If you are doing a unicode release build, then this will be a good start
"$(WXROOT)\lib\vc_lib\mswu";"$(WXROOT)\include"
Now you should have fixed your compiler errors.
Next you will get some linker errors ....
Maybe the errors are caused because you are using wxwidget 2.8 which is unsupported in VS 2010.
Try building wxwidgets 2.9 or use VS 2008. I had no problem building wxwidgets 2.9 on VS 2010.
Wxwidgets site home says "While this is still officially a development release because some API details are still not frozen, we believe that 2.9.3 can be used in production environment".
About what to do after successfully build it, take a look at this http://wiki.wxwidgets.org/Microsoft_Visual_C%2B%2B_Guide
You may want to use the tutorial of the section "Creating a new project from a sample" 'cause creating a project by hand is a little bit complicated as wxwidgets requires some extra configuration and not just to set and include and libs dir as most of the libs does.
Here is a video which shows how to start with Visual Studio.
Also you can take a look at samples directory and create your own project based on configuration of projects from that directory.
May be I am little bit late , But this will help newcomers to wxwidgets.
First of all Goto Wxwidgets directory(to download path) in my case it is "C:\Users\WxWidgets_all".
Now in that folder goto build directory.Now open Visual Studio Command prompt. If you have already installed Visual Studio then you can easily find it through start menu.
Now in command prompt go upto <Path to your wxWidgets>/build/msw/ . Now run Following Commands.
nmake /f makefile.vc--For Static Library and Debug Version
nmake /f makefile.vc SHARED=1--For SHARED Library and Debug Version
nmake /f makefile.vc BUILD=release--For SHARED Library and Release Version
After completing it , you will have dll or lib in "<path to wxwidgets>\lib\" depending on option provided in above commands.
Now include all files related to wxwidgets in your project.Mainly two paths are required to include "<path to wxwidgets>\inlcude\" and "<path to wxwidgets>\lib\vc_<lib\dll>".
After that link your wxwidgets generated library to your project.To do this inlcude "<path to wxwidgets>\lib\vc_<lib\dll>" to your project property in library section.
Now add below preprocessors to your project.
_WINDOWS
NDEBUG(For Release)
_DEBUG(For Debug)
_CRT_SECURE_NO_DEPRECATE=1
_CRT_NON_CONFORMING_SWPRINTFS=1
_SCL_SECURE_NO_WARNINGS=1
__WXMSW__
_UNICODE
After that add following library to your project from "<path to wxwidgets>\lib\vc_<lib\dll>"
wxbase30u_net
wxmsw30u_core
wxbase30u
wxtiff
wxjpeg
wxpng
wxzlib
wxregexu
wxexpat
wxmsw30u_adv
Based on your compiled options , library name can be little bit different.
I think I have covered all the steps to run wxWidgets.
Even if you got errors , You can check minimal project in "<path to wxwidgets>\samples"
directory. Try to run that project , and even if you got error then your wxwidgets is not built properly.
Hope this will help.

How to create Visual studio solution from make files?

I have a source code for a project with their make files. I want to create a Visual Studio (2005) solution from it. Is there any direct way to do this? can anyone help me please. I spent hours for searching, but couldn't find a way to do this.
Thanks.
Unfortunately, Microsoft removed this capability after VC++ 6.
If all you're looking to do is to build a Visual Studio project from a command line or script, you can use the devenv command to build using the settings in a project.
Something like:
devenv /build debug /project myproj myapp.sln
Ans starting with VS2010, C++ projects will use the MSBuild system, so you can drive builds using that technology.
If you really want a makefile, you'll need to write it up by hand (or maybe there's some 3rd party tool out there that I'm unaware of).
I'm not sure whether this solution can help you. Which I tried and it worked well in my previous projects. It need manually add the files.
Create a blank VS solution/project. Add the source files into that project.
Mark all source files as "Excluded from building". You can right click the files in project explorer and find the setting. So now nothing will happen when you build your project.
In project setting, find something like "Custom build step". Add the commands that invoke your original build command. (You may write different build command for debug/release ). You can also set the post-build actions such to copy your result to some folder....
Now you can edit and build source files.
For my experience, I can even debug it after setting the executable.
Hope this can help you.
If this is a one-off then it is easier to just create the VS project manually in visual studio.
If you are going to need to do this often look at ceating the project in something like cmake or Qt's .pro whcihc an generate makefiles and VS build files from the same defintion.
Do you want to use the makefile to build? You can create a project from existing source in VS 2005 and setup the project to use make to build (and the wizard will take you through all of this).
I am using VS2010.In order to build you can create a project from existing code. In VS2010 you can create project from existing code File->New->Project from Existing code. You can specify the other parameters and then ready with the solution. I did not go with make file but followed this approach which is working great.