How can I use Visual Studio to work with large non-VS codebase? - c++

I'm a fairly experienced C# dev, but have very little C++ knowledge. I have set my self a project to get a custom Firefox build running, and be able to control it from C# code.
I have got so far as getting and building the Firefox source, and creating a Visual Studio solution for the exe. This means I can now run via F5 in Visual studio. If I open a source file, I can set break points and have them hit.
What I'm not sure how to do, is load the entire source, as if I were working with a C# .NET solution. As I understand it, there are no project files with the Firefox source, as it is not windows specific source. I have followed an online example that suggests creating using 'project from existing code' option in VS, which resulted in VS grinding to a halt as there were so many files.
What are the steps to getting the code into an environment (preferably Visual Studio) that makes it simple(ish) to edit, debug and navigate the source code.
Note: Instructions I have been working through so far are here: https://cs.senecac.on.ca/~david.humphrey/writing/debugging-firefox.html

From you question, I beleive you are almost there. You have a working build ? That means you have :
A Solution File (*.sln)
A Project file (*.vcxproj or *.vcproj depending on yoru visual studio version)
With that in hand, what works best for me is this layout (adapted to your needs) :
Starting from a root folder of you liking, say MyProject
Create a new Empty solution there
Move the folder with your working build in a subdirectory, like MyProject\MyCustomFirefox
In Visual Studio "Add an existing project" and find your vcxproj file
In the same solution, create a C# project like you always do, in a directory at the same level as your FF build, like MyProject\MyFirefoxController
In short the solution file is pretty much alone in the root directory, and each project is in its own directory.
You will also need to adjust build options so that the output files (a DLL or an EXE) is seen by your C# project. While your are at it, make the C# project dependent on your Firefox build : it will instruct the msbuild to rebuild one if you change the other.
This will not work with the Express edition, I beleive. They are single language.

If you have a command line build path, which is creating a VS-debuggable executable, you could try adding all the source files to the project, but marking them 'exclude from build'. Then add a 'post-build step' to call the command line tools.
You may have to do a little more tweaking in the project properties to get the command line output recognized as the output to debug, but theoretically this could work.

Related

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.

C++ Project not building. No output files are produced

I have a solution with multiple C# projects included, and a single C++ project.
The C++ project is a .NET Framework bootstrapper that should produce a native executable file. I first built the project as a single project solution, however i'm now trying to migrate it into a master solution.
In order to migrate it, I added a new project called Setup to my existing solution called Master. I then added each header, source and resource by adding new files and copy-pasting the content.
I also changed the output directory to $(Configuration)\ so that it doesn't put the Debug and Release folders in the root folder of the solution.
Now, when I attempt to build the project, it says Rebuild All Succeeded, however when i try to debug it i get the following error
Unable to start the program ~ The system cannot find the file specified
Image of error shown when attempting to debug (F5)
If i navigate to the output directories, they are empty.
This is my first attempt at a C++ project, have i made a schoolboy error?
Does anyone know how I can get this working?
I found the answer. It was indeed a schoolboy error.
I was targeting AnyCPU, whereas i should have been targeting x86.
Now that i'm targeting the correct architecture, the project builds as expected.

c++ executable program test

I have been following some tutorials for c++ game programing. I am kind of new to c++ and I'm using Microsoft Visual C++ 2010 Express IDE. I'm working on creating a game, and when I run the program through the IDE, it shows the grass sprites as expected. But when I run the .exe file from the Release folder, it shows weird images. and when I run the .exe file from the debug folder I get a grey screen. Can anybody tell me why this is happening?
I hazard to guess that your sprite images are kept as data files in your project folder. With that I offer the following premise:
The default run-location from the Visual Studio IDE is the project folder of the project which you're executing. That is, normally it executes from the directory where your .vcproj or .vcprojx file is kept (and that is often one folder below your solution directory folder, where your .sln file is kept).
If your project runs correctly from the IDE, but fails to run directly from the release folder, it is highly likely you are relying on project data files (images in your case) that are kept along side your source files in the project folder. When run from the Release folder, those files are no longer visible because your the Release folder is your working directory; not the project folder.
There are a number of ways to solve this problem, each with its own merits. A few options are:
Post Build Step
Make a post-build step for your project that copies your data files to the $(TargetDir) location with your project. These files will then be visible in the same directory as your executable.
Benefit: Its easy.
Drawback: It will always run if you click "build solution" even if the data files are "up-to-date."
Custom Build Targets
Add your data files to the project and write a Custom Build script that performs the same copy, but also establishes an output dependency file(s).
Benefit: Almost as easy as #1, but a little more tedious.
Drawback: You may have a lot of data files and each will require its own custom build step. (Note: you can multi-select all the data files in your project, and if you're creative with the built-in macros you can have them all use the "same" build rules and commands).
Embedded Resources
Add the data files as custom resources to your executable.
Benefit: Your project no longer requires data files side-by-side with the executable since they are embedded in the resource table of your EXE module.
Drawback: Custom code is required to dynamically load the custom resources from your executable's resource table rather than off-disk. It isn't difficult at all to do, but is additional work.
There are other options as well, but I hope this gives you some ideas to start with.
I use VS2008 and try to answer your question. Right click on the project and select properties on the bottom of popup, then go to Debugging under Configuration properties. You can see command you run and arguments you pass in IDE. I guess you miss some parameters.

Handling images and sound files for a simple C++ project in Visual Studio

I am new to Visual Studio, and I am trying to figure out the best way to organize my projects.
I am writing an application using the sfml library, and I have various resources (images/sounds) that I am using. I dropped these into the project folder, and everything works fine when I launch my application from Visual Studio.
I am wondering though, how does this translate to when a program is deployed? If I go into my solution's debug folder, and try launching the exe, it is unable to locate any of the resource files. Am I suppose to tell Visual Studio to copy files to an appropriate directory, and if so how?
Thanks for any advice or links.
For slightly more complicated "deployment" scenario, you can use post-build scripts to copy the correct files into the output directory and even package it into a zip file, for example.
If you find yourself writing more than one page of batch you may want to consider the options below, because batch is a PITA to debug.
Recent MSVS project files are actually MSBuild files (just open the .vcxproj file in Notepad or Vim). For instance you can use the Copy task, invoke arbitrary programs using the Exec task, etc. It can be a bit more sophisticated than the batch script in post-build scripts. MSBuild 4 can use Property Functions making it quite expressive. Useful reference if you do this
For a "full blown" project, you'll want to roll a dedicated build system using a dedicated MSBuild file, NAnt or even higher level wrappers like Rake.
As a less popular alternative, in a previous project I built a small dedicated "builder" .exe project in the solution and have other projects depend on it. Then in the post-build scripts of the other projects I just invoke the builder projects with arguments to make it perform certain tasks. The advantage is that you can write C# (or F# or VB.NET) and not have to fight the build system (as much) and I think it works quite well for small-mid sized projects.
for my project, I direct everything into one directory.
Go to ur project configuration, change General->Output directory, General->intermediate directory, and Debugging->Working directory to one directory. The reason you cannot locate the resource files is because the debug directory is not the same as the output directory.

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.