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

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]

Related

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 to run “:compiler msvc” and “:comp msbuild” with Vim on Windows?

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

Building Qt cross compile from windows

[edit] Sorry, I out thought myself. I was using a .bat file to set environment variables that wasn't set up properly. When I went to a straight VS2005 command prompt, the configure ran fine. Sorry for the noise.[/edit]
I'm trying to compile qt-everywhere-opensource for embedded. I'm using MSVC2005. However, I guess I am confused on the process. I am getting compiler errors during the configure step. I thought the configure step was just trying to generate the needed makefiles, so I'm not sure what it is trying to compile.
I expected to modify the mkspec, but I'm not sure if during configure I should point to windows headers or sdk headers. I know during the build phase I should point to sdk headers, but I'm not getting that far.
I can change the errors by modifying INCPATH, there's also a QMAKE_INCDIR that seems to have an effect. It occurred to me that since I pulled the source, maybe configure needs to build the compiler tools, in which case I should point to MSVC headers. I also tried downloading the SDK and adding the path to it's bin folder to my path ahead of the -everywhere- source, but that didn't fix the problem and I don't want to jack up my system too much testing things.
Hope someone out there can help!
When you install Qt from source under Win32, you must:
Extend the PATH variable to include $(QTDIR)\bin, where $(QTDIR) is the directory where you installed Qt.
Open a Visual Studio command prompt in $(QTDIR); a plain old Windows command prompt won't do (unless you run vcvars.bat to load Visual Studio's environment variables; but opening a Visual Studio command prompt directly is better).
At this point, configure should run properly, after which you can run nmake.
I suggest that you clear your previous installation attempts from your system before you try these steps.

Using Visual Studio 6 C++ compiler from within Emacs

I'm just getting started with c++ development and I would like to use emacs to write the code and then compile and run it from within emacs using the visual studio 6 compiler. I have already googled around a bit but just can't seem to find an explanation of how this is done.
Any pointers?
Thanks for your help,
joerg
I have done this as a matter of course over the past few years. There are two ways to do this:
In the older versions of VS (including VS 6.0), there is a button to export the nmake file for the project. I do this on occasion and then use nmake to compile. I have
(setq compile-command "nmake debug ")
in my .xemace/init.el for this. You have to add the name of the nmake file, and nmake.exe has to be in your path. (BTW, I modified compilation-error-regexp-alist-alist to help moving through the errors. Let me know if you want this as well.)
Newer versions of VS do not have the button to export the nmake file. For these I call devenv.com. In this case I have
(setq compile-command "devenv.com /build Debug ")
in my .xemacs/init.el. You have to add the name of the .sln file. Although I did not do this with VS 6.0, I believe that it may work as well.
I am not sure if you would consider merely swapping the text editor inside of Visual Studio with Emacs, but here is a sf.net project which does just that.
http://sourceforge.net/projects/visemacs/
Checkout http://www.kuro5hin.org/story/2003/4/1/21741/10470
As a followup to Andrew Stein's answer, to do a command line build on Visual Studio 6 without using nmake, use the syntax:
msdev wspname.dsw /make "project - Win32 Debug"
Visual Studio is an IDE
It uses the cl.exe compiler underneath.
We use emacs with GNU make (gmake), which is wrapped with our own gmake wrapper executable and it works very well.
Just set up simple a keybinding to invoke cl.exe on .c/.cpp files that you are compiling to build your .obj files. We have one keybinding for initializing a compile in a folder, which will make all .obj files that are not up to date, but this is purely handled by GNU make.
We also have other keybindings, e.g. for linking the executable and starting it etc.
A bit off topic, but why are you using visual studio 6? The compilers (as well as stripped down IDEs) for the current version of visual studio are available for free from Microsoft.
In either case, you can invoke the compiler from the commandline so it should be as simple as setting up a makefile.
If you just want to set up a single file to compile then put this at the top of your file...
// -*- compile-command:"g++ test.cpp -g -lwinmm -o test.exe"; -*-
See my blog post on this
Of course you need to adjust to use CL.EXE and choose the appropriate arguments.
For launching make it's a bit trickier since you have to run make as if you were in the parent source directory, when you are editing a file further down the hierarchy.
The only way I know of to do that is to put this at the top of every file ...
// -*- compile-command:"nmake"; default-directory:"c:/projectroot/"; -*-

How to use Microsoft C++ compiler with NetBeans?

I was wondering whether it's possible to use Microsoft's C++ compiler and linker with NetBeans IDE?
If so, what's the best way of doing it.
P.S. I'm not interested in Mingw.
EDIT: Is it possible to get NetBeans to do error parsing (so that I can click on error and have NetBeans open the right file), intellisense, etc? I know NetBeans can work with g++ make files. Why not with nmake?
I am currently writing a plugin/toolchain to use Visual C++ on Netbeans.
You can find the project called VCC4N on source forge or on NetBeans plugins.
To be honest, I always do my spare time coding inside an ide (e.g. code::blocks, monodevelop, anjuta) or an editor (virtually always scite), and the compiling I do in a terminal via a makefile (handwritten, cmake, automake).
This isn't really a problem w.r.t. time to compile: F7 (or some other of the F keys) vs. (alt+tab, up, enter), where (alt+tab) and (up) are pressed nearly at the same time, but I get the great benefit of having up to full screen compiler reports, and often I am anyways testing my programs in a terminal. Also, it makes my code more independent of the IDE (ever tried to get a makefile from code::blocks for distribution purposes?).
The visual studio compiler is called cl.exe and the linker is link.exe. These are present in particular visual studio directories. From inside visual studio > project properties > C++ > Command Line, or by disabling "Suppress Banner" option there, you can find the command that visual studio runs. You can call these command lines from inside netbeans.
Getting all the file names into the list to compile may be more tricky. You need a build system for this. You can try to use the same mechanism that visual studio uses, but sorry my knowledge fails there. Alternatively, you can use CMake or some other build system. Then, whenever you add/delete a source file, you would have to update the CMakelist.txt to be able to compile.
You can get syntax highlighting, code graphing etc from netbeans without having a compiler installed I think (not certain, you may need cygwin or mingw for parsing). What you must do is create at least an empty makefile. If you want to use Microsoft's compiler then you either need to:
a) Write the makefile yourself to compile eveything using cl
b) Call on msdev from the makefile with the project name and it will compile everything
b) Call something like scons from the makefile to compile everything
I use netbeans to develop cross platform software, at this time though, I don't actually run the builds from netbeans.