How to run “:compiler msvc” and “:comp msbuild” with Vim on Windows? - c++

I have VS Express (2012) for Desktop. Am I also acquired the NMAKE.EXE program?
http://msdn.microsoft.com/en-us/library/dd9y37ha.aspx - The Microsoft
Program Maintenance Utility (NMAKE.EXE) is a tool that builds projects
based on commands contained in a description file.
When I run :make after running :compiler msvc, the shell returned the message “'nmake' is not recognized as an internal or external command,”. How can I compile a C++ program using these commands? Does the tool NMAKE.EXE have a relation to the 'nmake' command?
http://msdn.microsoft.com/en-us/library/wea2sca5(v=vs.90).aspx -
MSBuild is the new build platform for Microsoft and Visual Studio.
http://msdn.microsoft.com/en-us/library/dd293626.aspx - You can use
the MSBuild tool to build a Visual C++ application from the
command prompt. The build process is controlled by the information in
a project file (.vcxproj) that you can create and edit. The project
file specifies build options based on build stages, conditions, and
events.
Did I also acquired the MSBuild tool because of my VS Express for Desktop? When I use :make after running :compiler msbuild, the shell returned the message “'msbuild' is not recognized as an internal or external command,”. Does msbuild.vim have a relation to MSBuild tool?
The maintainer of msbuild.vim said, “I made the script for compiling
C# projects using .NET... I don't know if it will work for C++...”
How can I be able to compile by running :compiler msbuild before :make in Vim?
There are only two questions about compiling a program such as a C++ source file, on this question, and please answer with detailed instructions:
How to use :compiler msvc?
How to use :compiler msbuild?

When I use :make after running :compiler msbuild, the shell returned
the message “'msbuild' is not recognized as an internal or external
command,”.
Does msbuild.vim have a relation to MSBuild tool?
Yes.
To enable msbuild in Command Prompt, you simply have to add the path to the .net4 framework install on your machine to the PATH environment variable.
The following worked for me on Windows:
You can access the environment variables by right clicking on 'Computer', click 'properties' and click 'Advanced system settings' on the left navigation bar. On the next dialog bog click 'Environment variables,' scroll down to 'PATH' and edit it to include your path to the framework (don't forget a ';' after the last entry in here.
For reference my path was C:\Windows\Microsoft.NET\Framework64\v4.0.30319. [1]
Though I only use it for C# projects, I suppose it will work for C++ as well.
Remember that you have to run msbuild inside the project directory, otherwise msbuild will not be able to find your project.
For the sake of completeness, here a snippet of my vimrc (which the OP already got by email).
noremap <F4> :<C-U>silent make<CR>:redraw!<CR>
au FileType cs compiler msbuild
Patches and additions to this compiler script are always welcome, of course!
Regards
[1]: How do I run msbuild from the command line using Windows SDK 7.1?

A quick search came across this: Getting started - C/C++ programming with VIM, which may be a good starting point.
As user786653 mentioned, this is failing because your PATH doesn't include the directory nmake.exe is in. You can inspect your path with
:echo $PATH
Vim doesn't replace the underlying build functionality, it simply wraps it.
Start gVim from the Visual Studio command prompt
Create new files in a test directory:
test.cpp
#include <iostream>
int main() {
printf("hello world.");
return 0;
}
Makefile
all:
cl test.cpp
set the compiler (gVim)
:compiler msvc
compile (gVim)
:make
I don't have the msbuild.vim script, but by setting the "make program" to msbuild.exe
:set makeprg=msbuild
you can build, by running :make from a directory containing a solution (.sln) or project (.vxcproj) file, or you can use the msbuild.exe command line like this:
:make c:\Test\Test.sln /t:Rebuild /p:Configuration=Debug
After compiling, you can examine the output with
:copen
and navigate the errors with (n for next, p for previous, and r for rewind to the first error)
:cn
:cp
:cr

Related

Source environment variables before running cmake in VSCode

I am trying to build a large C++ framework in VSCode using CMakeTools and C/C++ Extensions in Visual Studio Code. I am browsing stackoverflow/github issues/any google suggestion and it looks like I am not the first person to encounter this, but I can not figure out for the life of me what am I doing wrong.
Here is the problem. I want to setup VSCode in a way to be able to build the framework (it is C++) right from VSCode using the built-in tools/extensions. Here is the process I was using up until now (in standard terminal in linux) and it also works in terminal run in VSCode:
cd /path-to-project-main-folder
source scripts/env.sh
cmake .
make -j 10
Now the problem is that when I set up VSCode, open the folder where the framework is, VSCode recognizes it is cmake project and gives me the opportunity to build it. Problem is that when I try to build it, it does not set up the environment first and therefore uses wrong cmake (not the sourced one but the default one build in server) and also wrong libraries and some of them are not even recognized.
The problem is in the first line:
source scripts/env.sh
where the environment variables are set and also PATHs to some libraries and programs. This line is not ran by VSCode before cmake and build.
Does anyone know a solution on how to configure CMakeTools extension to run:
source scripts/env.sh
line before running cmake and then make?
Thank you
I was looking into some solutions using tasks.json, settings.json files or creating my own kit. But no solution worked for me or I did not completely undestood the solution:
https://github.com/microsoft/vscode-cmake-tools/blob/HEAD/docs/tasks.md
https://github.com/microsoft/vscode-cmake-tools/issues/2243
https://github.com/microsoft/vscode-cmake-tools/pull/995
VSCode: Set environment variables via script
and man pages of CMakeTools, VSCode,...
VScode remote development: How can I run a build task to source environment variables before running the real build task? ---> but I use cmake
VSCode, how to source environment variable files like setup.bash?

Visual Studio custom build definitions for CLI built embedded C++

I have a build engine from ARM, called yotta. I have been working with the command line, where with the following commands, I can build projects and produce outputs.
$ yt target bbc-microbit-gcc
$ yt build
The compiled file is then accessible from build/bbc-microbit-gcc/source/Project-name-combined.hex
Is it possible to automate this CLI and build it into Visual Studio as a build definition? I know in Visual Studio Team Services / Team Foundation Server), you can define build steps using the command line, what is the local equivalent.
I am using Visual Studio Enterprise 2015. Any nudge in the right direction would be much appreciated.
EDIT:
I have seen the GCC4mbed project, but it would be good to use the new yotta command line build to do this. As far as VS is concerned is a command line tool which adds a built version of the project to a directory, a build tool? If so, then I know I can just follow the guides to add a custom build tool to VS however, it strikes me that VS might need a build tool to be in a certain standard to make intellisense etc work. In which case I might need to create a build tool on top of the existing yotta work and specify this to VS? Ideas would be muuch appreciated.
did you try to use cmake generators option which yotta provides (as it uses CMake)?
yotta build --help
-G CMAKE_GENERATOR, --cmake-generator CMAKE_GENERATOR
CMake generator to use (defaults to Ninja). You can use this to generate IDE project files instead, see cmake --help
The ideal solution which implemented partially as VS debug engine is still under development, just recently allowed gdb extensions. The tool on top of yotta for debugging is called Valinor (which is using progen: https://github.com/project-generator/project_generator).
Progen supports visual studio gdb (its very limited at the moment as explained later). I was able to use yotta debug with visual studio, but not without hacks (dated back in November-December 2015). The idea was to have a visual studio project with gdb setup and custom build commands (make,cmake or yotta) (in our case would be pyOCD settings, could be openOCD or jlinkGDB, and built environment for yotta).
As I recall, you commented on the issue on https://github.com/project-generator/project_generator/issues/98, and I haven't heard back from you. The problems I had I recall was I could not start gdb server (I had to do it manually), I could not make it to pass arguments to gdb (like load/reset/halt when starting the session).
I'll update my VS plugins and check if there were any updates in gdb plugins, I was told there should be.
I appreciate any help, either for progen or valinor to make this available for users.

How do I compile and run a C++ program from Vim?

Whenever I use the :!make % command, vim returns "make is not recognized as an internal or external command, operable program, or batch file." I have tried set makrprg=\"C:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 14.0\\VC\\bin\\cl.exe\". However, the same message appears. I believe the error may be in the path I have set, or the format of my statement; however, I am not sure if there is any other underlying cause.
I would greatly appreciate any input. Thanks in advance!
FYI:
I use a Windows 8 computer, and the compiler I typically use is the Microsoft Visual Studio 14.0 compiler.
! is a VIM command that invokes the shell. !make tells the shell to run whatever the shell can fund under the name make. If you want to use VIM's makeprg, you need to use the VIM command :make.
Having said that, setting makeprg to sonething that is not a real make-style program is probably going to work only in the very simplest scenario.
You can run the compiler directly with !cl %. You need to put cl.exe in your PATH and probably set up other environment so that cl can find libraries and include files.
This is because you do not have the make executable installed, which is what vim is looking for. If you're looking to compile on the command line with make, I would recommend switching from the Visual Studio compiler to MinGW
make is a Unix tool, and while it is also available for Windows (in various flavors, native, Cygwin, or MinGW), it is usually not what you will be using together with MS Visual Studio.
It is difficult to be specific, since you told us nothing about the project you are trying to compile, but I will try.
If your project is set up as a Visual Studio solution, you can compile it using devenv:
devenv /build release mysolution.sln
devenv /build release /project mysolution/myproject/myproject.vcxproj
Your project might also be set up for NMake (which is a make-like tool shipping with MSVC):
nmake [target]
The two commands above require the current shell to be properly set up, which can be achieved by starting a "Visual Studio Command Line" from the start menu, or running %VS120COMNTOOLS%\..\..\VC\vcvarsall.bat from whatever shell you happen to work from. (Adjust VS120COMNTOOLS to whatever version of MSVC you are using.)
Or your project might actually be set up using "real" makefiles, in which case I second Levi: It seems like make is not installed, or has not been added to your PATH environment variable.
make [target]

Eclipse CDT - how to run compiled .exe in external console (cmd.exe)

Is there a way to run compiled CDT program inside Eclipse IDE, but not in Eclipse terminal, but rather in new cmd.exe window? Some Run configuration or External Tools configuration?
Just like in QT, when you run compiled console application.
I googled this, on my mind, simple question, but (what is strange for me) I didn't find the answer.
From Eclipse CDT forum, from Mr Klaus:
Edit the Run Configuration:
On the Main page set C/C++ Application to:
${system_path:cmd.exe}
On the Arguments page add (adjust path and program name):
/C "start .\Debug\hello.exe"
On the Common page untick "Allocate console".
In the example above the console windows will close immediately if hello.exe ends. If you don't want that, use this version on the Arguments page:
/C "start ${system_path:cmd.exe} /K .\Debug\hello.exe"
Btw, you could use the same concept for External Tools configuration also!
In my case i was need to change arguments:
/C "start /WAIT .\Debug\hello.exe"

Easy way to run command line programs C++ - Windows 7

I am trying to find an easier way to test my command line application. It has arguments so I run it from a command prompt. My process is this:
Build
Go to the output
Open command prompt
Change directory
Run it
There has to be an easier way not only to debug but to open a command prompt in the current folder.
Thank you for your time.
If you go to the project properties, Debugging settings, you can set the working directory and parameters. If you use the same parameters all of the time, you can enter those in on that screen as well. Then just F5 or Ctrl+F5 to run.
Set a breakpoint at the end of the code to keep it from going away after it is done.
See Debugging with command-line parameters in Visual Studio
Alternatively, you should be able to use a shell script (or Python os.system()) to automate some of those steps.
To open a command prompt in the current directory using explorer, you can shift+right click->Open Command Window Here. That will save a little time.
If you're using Visual Studio, pressing F5 will run the code in the debugger, and Ctrl+F5 will run the code normally. Just remember to include a cin.get() statement at the end or the terminal window will close before you can read the output.
Other IDEs should have similar functions. Check in the Run or Debug menu.
EDIT: Sorry, didn't see that you're asking about running it with arguments. In VS, in Project Properties there are the Debugging settings. Within that, there is a field called Command Arguments. These will get passed to the application when you run it from within VS.
In project properties under debugging you can set the command line arguments (and environment variables) when debugging,
There is an extension called PowerCommands for Visual Studio 2010 that can be installed from Tools -> Extension Manager. It includes a Open Containing Folder and Open Command Prompt functionality that gets added to your right-click menu in the Solution Explorer.