How to Compile C++ Code Using Vim on Windows? - c++

First, I install Vim (Text Editor) in Typical type, on this Windows 7 laptop. And this user that I'm using is an administrator on this computer but couldn't open file in “Program Files” location for writing with Vim. So I change my :cd current directory to my $HOME.
Note: I already have installed Visual Studio Express for Windows Desktop. I just want learn how to use this.
Can I compile C++ code using Vim in Typical type alone on Windows?
If it's possible, then what's the process for compiling C++ code?
I have hello.cpp file but I can't compile it. I already use :comp gcc and :make and the output is: 'make' is not recognized as an internal or external command,
But when I use :comp msvc and :make, the output is: 'nmake' is not recognized as an internal or external command,
Now, what should I do? If it's not possible using Vim alone, how can I compile using Vim with the compiler on my VS Express for Desktop?

VIM is a great text editing tool,its like an awesome version of notepad.But it's not a compiler,neither has a build in compiler of itself.So In order to compile the program you'll need a separate compiler installed,like You can use MinGW.
See This :
How to use MinGW make with Vim on Windows

Vim is just an editor tool. To compile a program you need a compiler tool chain. The one that Visual Studio use is Microsoft Visual Studio C++ Compiler, so you could use it. Or install and use gcc.
The make file that you are generating is used by make utility to actually compile the code for you. There is a Windows version of this tool.
However I would suggest you to perform minimum steps to compile a c++ program manually for better understanding of the process:
Create source .cpp file -> Create object file with a compiler-> create executable with a linker
Good step-by step instructions for Visual c++ here
If you prefer to use gcc, these are good links how to install and use it on Windows.

Related

How to compile and run C++ code in VS Code easily?

So I just got into using VS Code. I am currently working with C++ and I am using Mingw as my compiler. So far I have been using the terminal at the bottom of VS Code to compile and run like this:
g++ program.cpp then doing ./program.exe
The process of using the terminal at the bottom is time consuming especially when I need to compile and run code frequently. This is also annoying when creating classes when you have to compile multiple files into .o extensions etc.
Can anyone help with this process? Am I doing something wrong, should there be an easier way? Maybe a makefile?
Thanks!
If you want to compile and run C++ code in visual studio code(Vs-code) in windows. This include following steps.
Download Visual studio code.
Go on Add extension Type C++ and install "C/C++" by Microsoft.
Go to Visual Code studio docs for "C++" OR https://code.visualstudio.com/docs/languages/cpp
Install MinGW-x64 vis MSYS2 website and run this on shell "pacman -S --needed base-devel mingw-w64-x86_64-toolchain"
Then go to windows setting and look for Edit environment variables for your account. Then in advance settings >> Environment Variable.
In "system variable" choose path and edit it and add a new path.
You can find a new path in your directory where you have installed the MinGW-x64. you might find it in C:\msys64\mingw64\bin. or where ever you have installed it.
When you have added the new path then go to any shell/cmd and Type g++ --version
if you get the version then you have succeded.
If you find something like command not recognized then please check where you have done wrong or skipped any step.
Otherwise startover.
thanks--

Setup C++ on Visual Studio Code [duplicate]

I am programming in C in Visual Studio Code, but I can't compile, as VSC only offers three compilers built in - Node.js, C# Mono, and Extension development. After a little bit of digging I came across the Visual Studio Marketplace. This seemed like the right sort of thing, but only four uncommon languages were there.
I can only assume that C debugging support is built in, I just can't find it or I am going the wrong way about doing it. I attempted to create a new launch.json (the manifest that seems to hold the compiling/debugging settings for each file) and manually entering the GCC binaries that I have, but that didn't end up working. I'm currently stuck manually compiling the C source file I am working on through command prompt.
Would really help if someone could point me in the right direction on what to do.
tl;dr - Help from anyone debugging C in Visual Studio Code
Windows 8, if that matters
Cheers!
Caution
A friendly reminder: The following tutorial is for Linux user instead of Windows
Tutorial
If you want to debug your c++ code with GDB
You can read this ( Debugging your code ) article from Visual Studio Code official website.
Step 1: Compilation
You need to set up task.json for compilation of your cpp file
or simply type in the following command in the command window
g++ -g file.cpp -o file.exe
to generate a debuggable .exe file
Step 2: Set up the launch.json file
To enable debugging, you will need to generate a launch.json file
follow the launch.json example or google others
Step 3: Press (Ctrl+F5) to start compiling
this launch.json file will launch the configuration when you press the shortcut (Ctrl+F5)
Enjoy it!
ps. For those who want to set up tasks.json, you can read this from vscode official (-> TypeScript Hello World)
Press Ctrl + Shift + P to pull up the Command Pallette, and Type ext install cpptools. It will install everything you need to debug C and C++.
Debugging in VS code is very complete, but if you just need to compile and run:
https://code.visualstudio.com/docs/languages/cpp
Look in the debugging section, and it will explain everything.
There is a much easier way to compile and run C code using GCC, no configuration needed:
Install the Code Runner Extension
Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.
Moreover you could update the config in settings.json using different C compilers as you want, the default config for C is as below:
"code-runner.executorMap": {
"c": "gcc $fullFileName && ./a.out"
}
For Windows:
Install MinGW or Dev C++
Open Environment Variables
In System Variable select Path -> Edit -> New
Copy this C:\Program Files (x86)\Dev-Cpp\MinGW64\bin to the New window.
(If you have MinGW installed copy its /bin path).
To check if you have added it successfully: Open CMD -> Type "gcc" and it should return:
gcc: fatal error: no input files compilation terminated.
Install C/C++ for Visual Studio Code && C/C++ Compile Run || Code Runner
If you installed only C/C++ Compile Run extension you can compile your program using F6/F7
If you installed the second extension you can compile your program using the button in the top bar.
Screenshot: Hello World compiled in VS Code
Just wanted to add that if you want to debug stuff, you should compile with debug information before you debug, otherwise the debugger won't work. So, in g++ you need to do g++ -g source.cpp. The -g flag means that the compiler will insert debugging information into your executable, so that you can run gdb on it.
You need to install C compiler, C/C++ extension, configure launch.json and tasks.json to be able to debug C code.
This article would guide you how to do it: https://medium.com/#jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6
EDIT: As of ~March 2016, Microsoft offers a C/C++ extension for Visual Studio Code and therefor the answer I originally gave is no longer valid.
Visual Studio Code doesn't support C/C++ very well. As such it doesn't >naturally support gcc or gdb within the Visual Studio Code application.
The most it will do is syntax highlighting, the advanced features like >intellisense aren't supported with C. You can still compile and debug code >that you wrote in VSC, but you'll need to do that outside the program itself.

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]

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.