Trying to compile and debug C++ from Vim - windows

I've been learning C++ and Allegro using Code::Blocks on Windows XP, and recently felt like learning Vim. However, I have no idea how to compile a program "manually" (i.e., without using an IDE).
For compiling from within Vim, I have gotten as far as setting the makeprg to gcc, but I understand I also need a makefile. What is that and how do I write one? Is it with the makefile that I can use libraries such as Allegro?
Also, I've gotten quite fond of the CB debugger (I'm using gdb). Is it possible to do something similar with Vim?
Thank you.

Look at MinGW. I would avoid Cygwin if you only need gcc and make. You'll want both MinGW and MSYS. MSYS has a windows port of make.
If you're interested in more unix utlities for the windows command line I recommend CoreUtils.
For learning make see the manual

You don't necessarily need a Makefile, but it's the preferred (and possibly sanest) way of compiling code on UNIX-like systems.
I don't know if GNU Make has a Windows port, but you can probably run it under Cygwin.
To learn more about GNU Make and Makefiles:
make (Wikipedia)
GNU Make
GNU Make Manual
Also, see this question: compile directly from vim
Mandatory edit: I don't use Windows or Cygwin. You might want to take epochwolf's advice on that department.

I'm not in expert in makefiles and debugging but I know that Vim lets you do many things.
For example if you want to compile a file with gcc, it's not very different from the usual way. In normal mode type:
:!gcc file.c -o file
In fact you can use (almost) every system command just by adding "!" in front of your command.
gdb also works with Vim
:!gdb
I hope it will help you.

To integrate vim with devenv, you can use devenv from Visual Studio to complie the project in vim. The command as follows:
Devenv SolutionName /build SolnConfigName [/project ProjName [/projectconfig ProjConfigName]]
Typicatlly, the devenv should located in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE. Set it to the path of environment to make it callable from vim. We also need to make vim recognize the error messages thrown by the commandline build tool devenv. Just put the following lines in your vimrc:
" Quickfix mode: command line devenv error format
au FileType cpp set makeprg=devenv.com\ /Build\ Debug\ *[SolutionName]*
au FileType cpp set errorformat=\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %m
Then You can use :make to compile and build the program from vim.

EDIT1: A few bookmarks you might find useful:
GNU make tutorial (this one uses gcc and make, so it should be right up your alley)
Another one
Win port of some of GNU utils Can was mentioning; I personally use these and haven't had any problems with them on Windows platform.
Yes, you can compile without the makefile. If your program is simple (for example, one file only) you can compile by calling the compiler and including the name of the program in the same line (don't remember how it goes with gcc). Of course, to make things easier this can be mapped to a key within vim, so you don't have to jump to command prompt and back.
If you are working on a bigger project, which consists of several files and such, than a makefile is useful. It will "search" through the files, determine dependencies, include them in the build, maybe put the source files in one directory and the resulting exe file in the other and such. So it is more of a linking and building system than just compiling.
Although the GNU make mentioned in Can Berk Guder's answer is a popular one, there are quite a number of other tooks for "building makefiles" ("makefile" has become a type of synonym for that kind of operation) - here, you can see some other options on this link. Due to its part in history vim has good support for :make, but others can be easily used as well (there are a lot of texts on this subject on VimWikia.
Well, that's it. Just my 0,2 euros :)

As long as you have GNU-make installed (with cygwin or mingw under windows), you don't need to write a makefile for single-file projects. Just run :make from vim, and that's enough.
If your project is made of several files, then you will have to write a makefile (or any equivalent for scons, aap, (b)jam, ant, ...), tune your &makeprg in consequence, and finally call :make from vim. See the relevant category in vimtips. You can of course run the compiler as you would have ran any other external tool, but you would loose the possibility to jump to the line(s) of the error(s).
NB: if you are using the win32 version of vim, and gcc-cygwin, you'll need to translate the error messages obtained. I used to maintain a perl script for this purpose, it is now part of a bigger suite (still in beta stage)
Regarding your question about debugging, it can't be done from vim under windows for the moment. The only debugger that can be integrated so far is gdb, but under linux only ; see the pyclewn (.sf.net) project.

I'm not sure about debugging, but I know an easy way to compile and run the current program as I wrote a small vim plugin to do so.
http://pastebin.com/qc5Zp2P7
It assumes you are using the g++ compiler. If not, just change the name to the compiler you're using. Save this file to whereveryouinstalledvim/ftplugin/cpp.vim
In order to compile and run the currently open program, just type shift-e while in non-editing mode.

Related

Compile for windows from WSL with CMake setup

So I get a bit confused on the many possibilities here. I have a CMakeLists.txt, which is hopefully all ready to be used. I am using WSL in the Windows Terminal and switching between microsofts cmd.exe and ubuntu wsl terminal on which I want to be able to run the compile command. So on which side do I need to run cmake and what build-files should I generate or what compiler should I use?
Main goal is having a simple compile cmd in vim on wsl, that compiles the exe for windows.
I tried to setup the build files with cmake from the wsl-side with the mingw cross-compiler, but during project setup it failed to find glfw3 configurations, which felt rather weird since it should use windows glfw files anways, right(?)
Next closest try was to create the build files with cmake from the cmd-site with the cl.exe compiler and ninja. That succeeded, but when now running ninja on the wsl side I get a lot of warnings concerning the unknown cmd option \O0 and fatal errors concerning not being able to include simple files like float.h, stdio.h, Windows.h etc. .
All my current search led me to this point, but I am not able to come up with a solution. So I would love to get some help. I want to stick mainly on the linux side, when programming but in the end have an executable for windows.

How to compile a file as c++ in flex windows?

I am using "flex windows(lex & yacc)" in windows 7 32 bit, where the lex programs can be compiled, built and run by automatic call on command prompt, using some tools. But by default, it compiles files as c...which i want to change so as to compile as c++. is there any way to do that without using any cmd command?
Be aware that flex is a C or C++ code generator. And so is bison. They both emit some C++ (or C) code from some other kind of input.
So, compile on the command line. BTW, all C++ compilers I heard about are command line programs.
is there any way to do that without using any cmd command?
In practice, code your Makefile to drive flex, bison, g++ or clang++ (or any other C++ command line compiler you have). Then you'll just build your program using make -and you could and probably should configure your IDE or source code editor (e.g. emacs or Visual Studio Code, etc...) to build your program that way (maybe with a single mouse click or a single keypress).
Or use some other build automation tool. For example, ninja.
How to configure your IDE or editor is of course specific to that editor. I am using emacs (on Linux) and my ~/.emacs configuration file contains:
(load-library "compile")
(global-set-key [f12] 'recompile)
So I simply compile by pressing the F12 key. That fires a make process.

Could not configure a C compiler (Windows)

On a Windows system, currently I'm trying a waf configure on a directory of code, and it spits out the error "could not configure a C compiler."
Now, I'm 100% certain that I have gcc and g++ installed and in my path because when I type gcc --version, it gives me the current version information. (I'm using mingw and the gcc/g++ are in the /bin subdirectory).
In the author's code directory there is a wscript file which looks like
C_COMPILER = 'gcc-4.7'
CPLUSPLUS_COMPILER = 'g++-4.7'
Now, I have tried changing the strings to simply gcc as well as gcc-4.8.1 (since my current version is 4.8.1), but it still says could not configure compiler.
I tried reading one solution on this same site that looks related, but the solution was on ubuntu and trying to work through those commands didn't help
could not configure a c compiler
I'm at the end of my common sense here after making sure I have gcc and g++ installed, trying different strings in the wscript file trying to get it to recognize I have them installed, and could use some help, thanks.
Edit: I've now tried simply deleting the lines in the wscript file where it changes the compiler name, and suddenly waf configure goes through, but the waf build fails saying things like it can't find really basic things like include vector. The output says it's defaulting to msvs (microsoft visual studio) whereas the author says gcc/g++ is needed; maybe this is the issue but how do I get waf configure/build to use g++/gcc as default?

Build a UNIX make project in Windows

I have a C project with a Makefile, I want to build it on windows. I downloaded GNU Make installed. And I have VS2010. The make file has UNIX commands like cc, nvcc and g++. I have to replace them with MS C, CUDA and C++ compilers right? What are the command line commands I have to replace them with? Is there anything I need to install?
I want to do it without Cygwin (it's installation is taking forever), otherwise Cygwin is the fallback method.
You don't actually have to replace any of those commands with the Windows-variant. A working compiler will still compile source code just fine even if it isn't the defacto compiler for an operating system.
I hear ya on the Cygwin bit--you can just install a Minimal GNU environment using MinGW and that'll give you everything that you need to be able to the utilize the Unix makefile.

How do I create an executable file with OpenCOBOL?

Upon finishing a COBOL program, how do I compile it into an executable file that may be run on other PCs? I'm using OpenCOBOL via cygwin.
Check out this getting started page from the user manual for OpenCOBOL:
But in case the link is broken, just do this:
$ cobc -x hello.cob
$ ./hello
cobc is the compiler. hello.cob is the source file. The output is simply the file hello which can be run by calling ./hello. The -x option is necessary to build an executable.
However, with all compiled programs, it is compiled for the machine is was built on. It will work on machine with similar architectures, but you don't true cross-platform ability unless you're using an interpreted language like Python or Java.
If you compile with Cygwin, the target computers also need Cygwin, or in particular the cygwin dynamic libraries along with the OpenCOBOL runtimes.
Many times, you can also compile under MinGW, which lessens the dependencies, but also lessens the available POSIX features.
Easiest path, install OpenCOBOL and Cygwin on the target machines, and you'll be good to go, otherwise you'll need to produce release packages with all the dependencies and instructions for PATH settings.

Resources