QtCreator add include in project by single click - qt-creator

I have found very cool possibilities in QtCreator which is:
prolongate signature changes between declaration and definition for C++ methods,functions
find symbols usage
Is it possible "append" include file for specific "type name" of "function"?
(Like Visual Studio VS assist plugin)

Related

Visual Studio 2015 appears to complie VB code with C#

Steps to Reproduce:
Create new Visual Basic Analyzer with Code Fix (NuGet + VSIX)
From VSIX project add New Item...
Extensibility/Custom Command called "CustomCommand"
Not required but helps see the issue set the following options
Option Explicit Off
Option Strict Off
Option Infer On
Build Solution
Several files are added and you get 381 warnings for the ComandComand.vb and CustomCommandPackage.vb files. The warnings are "Type or namespace definition or end-of-line expected", "Newline in Comment", "Type Expected" , "Too many characters in character literal"., "Comma Expected". None of which make any sense in a VB file. If I included these files in a C# project and rename them to .cs I get the exact same errors. The errors make no sense for a .VB file.
The example above is complete and shows a bug in Visual Studio 2015. In the VSIX project file there are two lines that include the word "CSharp" that must be changed to "VisualBasic" otherwise the VB code will be compiled by the C# compiler. To edit the file, unload the VSIX project from within Visual Studio, replace occurrences of "CSharp" with "VisualBasic", Save and reopen the solution the 381 Errors/Warning will go away.

Additional include directories in VS2010 for an "empty C++" project

I am creating a sample project (a Hello, World!) with libevent.
(Just for note: This question has got nothing to do with libevent but with Visual Studio. :-))
I compiled the library separately outside but under Visual Studio environment.
Wanted to test with a sample code and I started with creating a new project in VS2010.
I selected Visual C++ --> General --> Empty Project.
When I selected the project properties to add the 'additional include directories' I do not find
C/C++ node where it is usually added.
This is how it looks:
However, when I select Visual C++ --> Win32 --> Win32 Console Application I will be able to see the C/C++ node and add the additional include directories.
I read this post and understood I can add the required directories in VC++ Directories --> Include Directories. But yet it's not so very advisable to change the default setting.
Is there any other way I can add additional include directories without selecting the C++ --> Win32 --> Win32 Console Application project? Just want to know.
You need to have at least one .cpp file added to the project to be able to see the C/C++ settings. Otherwise, being an empty project, VS does not know what language you are using
You have to have a C++ project. It doesn't need to be a console application; you could also create a static library (.lib) or dynamic library (.dll).

Removing all unused references from a project in Visual Studio projects

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?
In your answer, please specify which version of VS the solution applies to.
If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...
http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html
This feature does not correctly handle:
Dependency injected assemblies
Dynamically loaded assemblies (Assembly.LoadFile)
Native code assemblies loaded through interop
ActiveX controls (COM interop)
Other creative ways of loading assemblies
All you need is stone and bare knuckle then you can do it like a caveman.
Remove unused namespaces (for each class)
Run Debug build
Copy your executable and remaining namespace references to new location
Run the executable
Missing Reference DLL error will occur
Copy required DLL from Debug folder
Repeat 4-6
Gu Gu Ga Ga?
Throw your stone
You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.
The Resharper extension will do this for you.
This extension supports Visual Studio 2005 through 2017.
While the compiler won't include unused assemblies, extraneous using statements and references slows down Visual Studio and Intellisense, since there's more code the tools have to consider.
You can try the free VS2010 extension: Reference Assistant by Lardite group. It works perfectly for me. This tool helps to find unused references and allows you to choose which references should be removed.
In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn´t such a function.
The only way to do it in a C# project (without other tools) is to remove possible unused assemblies, compile the project and verify if any errors occur during compilation. If none errors occur you have removed a unused assembly. (See my post)
If you want to know which project (assembly) depends on other assemblies you can use NDepend.
With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.
Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.
For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:
reference is removed in the project
project is compiled with msbuild
check for build errors
restore removed references if there were build errors.
For anybody coming here looking for Visual studio 2012:
Download and Install Reference Assistant for Visual Studio 11
Later you can do:
In Visual Studio 2013 this extension works:
ResolveUR
Some people suggested to use an awesome tool - Reference Assistant for Visual Studio. The problem is that VS2012 is the latest supported Visual Studio. But there is the way to make it work in VS2013 as well ;)
And here is how:
1) Download Lardite.RefAssistant.11.0.vsix
2) Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip
3) Unzip and open the extension.vsixmanifest file in the text editor
4) Find all occurences of InstallationTarget Version="[11.0,12.0)" and replace them with InstallationTarget Version="[11.0,12.0]" (note the closing bracket)
5) Save the file and zip all files so they are on the root zip level
6) Change the extension of the new zip to vsix
7) Install and enjoy :)
I've tested it with VS2013, thanks source for the tutorial
EDIT
Add to support VS 2015 Community Edition
<InstallationTarget Version="[14.0,15.0]" Id="Microsoft.VisualStudio.Community" />
Meaning of the brackets
[ – minimum version inclusive.
] – maximum version inclusive.
( – minimum version exclusive.
) – maximum version exclusive.
[Update] This feature is only available for .Net core projects.
This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1.
This option is turned off by default, but you can enable it under menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer (Experimental). Once the option is enabled, the Remove Unused References command will appear in the right-click menu of a project name or dependencies node.
You can use Reference Assistant extension from the Visual Studio extension gallery.
Used and works for Visual Studio 2010.
In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).
If it is available you can find it in the project context menu:
You get to choose what to do with each finding.
Read more about it here.
Pro-tip: Check if your project compiles and runs correctly after applying this. In my experience it doesn't check whether a dependency is used at runtime, for instance.
Using DevExpress, I follow these instructions:
In VS, go to DevExpress - Editor - Code Cleanup. Under Rules, check 'Remove unused namespace references'. Click OK.
Right-click on the solution, and choose 'Code Cleanup'. The cleanup runs for a few minutes, and finishes.
Build your application
The following method does not depend on any 'add-on's and is not very painful.
Step through each of your source files and
Select all (Ctrl-A)
Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.
Scanning each file looking for '0 reference' takes only a second.
Scanning an entire project takes only a couple of minutes.
In VB2008, it works this way:
Project>Add References
Then click on the Recent tab where you can see list of references used recently. Locate the one you do not want and delet it. Then you close without adding anything.

How to convert a regular win32 (VC++ vcproj) project to a Qt project?

How can I convert existing vcproj files to project files that the QT add-in to Visual Studio recognizes and treats as valid Qt projects?
Should I just bite the bullet and create new projects to replace the old ones, and then add existing code?
I am using QT 4.6 and VS 2008 with Qt Add-in 1.1.2.
Original vcproj files have NOT been generated with QMake.
Using Visual Studio 2010+ it is now possible to convert a project to a Qt Addin project, using the "Convert project to Qt Add-in project" feature of the Qt Addin.
The following steps are from the work around in bug QTVSADDINBUG-27. This was tested with Visual Studio 2010, but should work similarly with new versions:
Right click on your project in "Solution Explorer", click on "Unload Project"
Right click on your project in "Solution Explorer", click on "Edit .vcxproj"
Add line <Keyword>Qt4VSv1.0</Keyword> between the tags <PropertyGroup Label="Globals"> and </PropertyGroup>.
Right click on your project in "Solution Explorer", click on "Reload Project"
Right click on your project in "Solution Explorer", click on "Convert project to Qt Add-in project"
If you have header files in your project you are going to have to follow the instructions in this answer to trigger the generation of moc_* files in the "Generated Files" folder.
For editions of Visual Studio older than 2010 see this work around.
Here's the response from Nokia support engineer, name removed for privacy protection:
Hi,
Thanks for the suggestion. I have
passed it on to our developers who
will consider implementing it for a
future release. You can track it using
the following link:
https://bugreports.qt.io/browse/QTVSADDINBUG-27
Regards,
Support Engineer, Qt Development Frameworks, Nokia
>
Original question
We have quite a few legacy vcproj projects that we wish to add Qt to it.
Currently, we don't have an easy way to convert these legacy vcproj
files into a Qt-enabled vcproj.
What do I mean by Qt-enabled?
On a non-Qt-enabled vcproj, when I right click on the project name
under Solution Explorer, all options related to Qt are all grayed-out.
It would be really useful if Nokia adds this capability to VSTD Add-in.
Stackoverflow fellows, if you would like to see this feature implemented by Nokia, please help vote for it at the link above! =)
I use Qt VS Tools instead of Qt VS Add-in.
Instructions
In the .vcxproj file, make the following modifications:
Add <Keyword>Qt4VSv1.0</Keyword> into <Project><PropertyGroup Label="Globals"> (in accord with Benjamin's answer).
Add the following element into <Project> (the root element):
<ProjectExtensions>
<VisualStudio>
<UserProperties MocDir=".\GeneratedFiles\$(ConfigurationName)" UicDir=".\GeneratedFiles" RccDir=".\GeneratedFiles" lupdateOptions="" lupdateOnBuild="0" lreleaseOptions="" Qt5Version_x0020_x64="5.6-msvc2013_64" MocOptions="" />
</VisualStudio>
</ProjectExtensions>
Note that the value of the attribute Qt5Version_x0020_x64 should match the name of a Qt version in Qt VS Tools. You may set an arbitrary value and then fix it in Qt Project Settings in Visual Studio.
My setup
Visual Studio 2013
Qt VS Tools 2.0.0 Beta (apparently a rebranding of Qt VS Add-in)
Qt 5.6 msvc2013_64
Notes
When I follow Benjamin's answer and try to "Convert project to Qt VS Tools project", Visual Studio gets stuck.
The only Qt specific functionality I've tried with the project is exporting a .pri file.
I have reverse engineered the necessary modifications from a fresh VS project created using the template Qt GUI Application.
Converting a regular VC project to a QT project should be easier and the error message displayed by the Qt plug-in when attempting to add a Qt class to a non-QT project is exceedingly unhelpful especially when considering the actual differences between a Qt .vcprojfile and a non-Qt one are minimal. It is often the case that you have existing legacy code built into libraries and Dll's. During migration to Qt you may wish to add a Qt class to an existing module which was previously a non-Qt project. If the module has relatively few source files you may find it easier to re-create the project as a Qt project. If it has hundreds of source files or is complex in other ways it is often be easier to simply hand edit the .vcproj file. By comparing a regular .vcproj with a Qt one it is easy to see what the differences are:
A Qt project has a Keyword="Qt4VS1.0" immediately before
TargetFrameWorkVersion line near the top of the file.
At the bottom of the file you need to add the required Globals (in
particular QtVersion.Win32)
You will need to add the locations of the Qt header files to the
include paths (for all build configurations). You can do this later
via the visual studio user interface
For an exe or dll you will also need to add the Qt library locations
and library files to the linker options.
You actually only need to do 1 and 2 in a text editor and then you can reload the project in Visual Studio.
After setting up include paths and libraries you should be able to build the project as usual. Next you can add a new Qt class or convert an existing class into a Qt class. To do the latter first remove the source files for the existing class from the project otherwise the Qt add class wizard is likely to complain.
The above steps are intended for experienced users and you should always back up existing projects .vcproj files bfore hand editing. Simple mistakes here can render the file unusable.
More answer from Qt support engineer; I think the first pargraph is helpful as I didn't know that before:
i think this is a misunderstanding
really. If you have a Qt project
generated with the Add-in, then you
can change this project into a qmake
generated Qt project - and vice versa;
If you have a .vcproj that has been
generated by doing qmake -tp vc, then
you can convert this to a Qt Add-in
project.
There is no functionality to convert a
regular win32 project into a Qt
enabled project, so the suggestion
created is still valid, see:
https://bugreports.qt.io/browse/QTVSADDINBUG-27
I can see from the history of this
report that it has been closed and
re-opened again, so I assume there was
a misunderstanding regarding what you
were looking for in the first place.
If you create a .pro for you project
using qmake, then you should be able
to create a .vcproj file using 'qmake
-tp vc' on that .pro file. The qmake manual is available here:
http://doc.qt.io/qt-5/qmake-manual.html
I hope this helps.
Regards,
--
Support Engineer, Qt Development
Frameworks, Nokia

-D for visual studio

I have to compile one executable on Windows, the first time. I'm using visual studio 2003 and want the equivalent of a command line macro definition.
What's the VS equivalent of g++ -Dthismacro=1?
Update
For VS 2003, I found it this way. Right clicking on the project, I go to "Properties". From there, it's Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
since you seem to be coming from a linux-flavor, why not use the commandline: the argument for macro definition is actually the same, except the compiler executable is cl and not g++:
cl -Dthismacro=1
although the documentation says the convention is to use /D, most (all?) arguments can be used with the - switch.
When using visual Studio, you can verify this: if you add a preprocessor symbol the way earlz suggested, this can be seen as /D"thismacro=1" option under Configuration->C/C++->Command Line
If you right click on the project, it should bring up a project configuration page. Goto "build" and then you should be able to define preprocessor symbols.. (hopefully it works the same in 2003 as 2008)

Resources