How to do automatic g++ compilation after each save? - c++

I'm writing simple C++ code (one "cpp" file). Now I compile my program by manually running g++ from command line. But I was wondering if there is any way to do automatic g++ compilation after each save of file.
I'm using Ubuntu and Sublime Text 3.

There are also much fancier solutions available:
YouCompleteMe compiles and analyses the code in background while you type, for Vim
Emacs-ycmd is one of several ports to Emacs
RStudio IDE does the for C++ code (when though it is an R environment)
You can 'cook' similar things much simpler by using Linux kernel hooks in userspace to react whenever a file you watch is saved. Here you'd simply call g++, or as others already said, you probably want make.
One of the hooks is inotify; there are several Ubuntu packages accessing this (which I have yet to use). Here is one:
edd#max:~$ apt-cache show entr
Package: entr
Priority: optional
Section: universe/misc
Installed-Size: 61
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Original-Maintainer: Yuri D'Elia <wavexx#thregr.org>
Architecture: amd64
Version: 2.6-1
Depends: libbsd0 (>= 0.6.0), libc6 (>= 2.4)
Filename: pool/universe/e/entr/entr_2.6-1_amd64.deb
Size: 11310
MD5sum: 818b54114577b8d15d619577acf52c97
SHA1: b8422ee12546843f3357c1114bf1f5eeea8c863e
SHA256: ada9aa2ea218fc9f7a255b576d5069dda06da369ecc4000bb596e398b04eeafd
Description-en: Run arbitrary commands when files change
The Event Notify Test Runner (entr) runs arbitrary commands when
files change. Changes are detected through the kqueue/inotify
kernel interface.
Description-md5: 52fe22e37b3719b7c736bf46a6d8c9b7
Homepage: http://entrproject.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
edd#max:~$

Create a makefile. Most editors have a shortcut keypress that executes make.

You can use make command.
Example: Imagine the file name is test.cpp then you can execute the following command:
make test
It will automatically compile using g++ as the extension is .cpp

If you are using VS Code, you can install the extension C/C++ Compile Run for the VS Code.
Once you change the code, just click F6 for executing it without passing any additional arguments, or F7 to type the passing arguments in the Command Palette, which will be passed to the code at the beginning of the execution.
C/C++ Compile Run

Use a proper build system that integrates with your IDE/editor.
There are many to choose from. My personal preference is SCons (http://scons.org/) but there are also options like CMake (https://cmake.org/) or autotools (https://en.m.wikipedia.org/wiki/GNU_Build_System) available or simply a plain Makefile.
And there are many more less known ones as well.
Check them out and pick what suits your needs best.

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--

Using Fortify SCA (not outdated "HP Fortify") on Qt code

I've been trying to research this for a while and my limited experience with compiling is hindering my ability to figure it out.
Basically, I have some code which is being written in Qt Creator, then built with these build steps:
qmake.exe [project name].pro -spec win32-msvc "CONFIG+=qtquickcompiler"
jom.exe in C:\eclipseworkspace\[project directory]
I'd like to use the Fortify SCA (Static Code Analyzer) to automatically scan this code for vulnerabilities, but most of its user-friendly features are designed towards Java. I haven't given up, though, because Fortify does claim to be able to scan C++ code that uses 3rd Party Compilers (which I assume Qt falls into that category).
(Page 37 of this document)
As a preliminary step to running Qt Creator on my actual code, I've wanted to see if I can at least get it to run on any Qt sample project, to see what the steps to do that would be.
I'm using Qt 5.12.7
on a Windows 10 OS
with the MSVC2017 32bit compiler,
but I feel any correlation between Qt and Fortify that works will be enough to set me off in the right direction.
Or perhaps my optimism is misplaced and I just don't understand the limitations of what I want to do. Either way, it'd be nice to know.
I have found this to be easiest on Linux. I think this solution translates to Windows.
You must inject the sourceanalyzer into your compiler command.
For example, I run cmake to configure my projects.
export CC="sourceanalyzer -b <your_project_name_here> gcc"
export CXX="sourceanalyzer -b <your_project_name_here> g++"
cmake <bunch of my cmake definitions> <path_to_src>
# I do a clean to remove what sourceanalyzer picks up during configuration time tests.
sourceanalyzer -b <your_project_name_here> -clean
sourceanalyzer -b <your_project_name_here> -scan -f scanResults.fpr

VS2017 - How to configure project for remote debugging

I want to create an MP3 player on my Raspberry Pi (OS: Raspbian). The problem is, that i don't have any experience with Linux programming, and I'm having a huge problem with project configuration, becouse of cross platform compiling. I want to add two liblaries:
mpg123
libao
I'm able to compile the code on linux machine with gcc -O2 test.cpp -lmpg123, but I can't force Visual Studio to make see those libs.
I'm also having a problem with using wiringPi lib while I'm trying to remote debugging my program, since I have to run it as root for GPIO configuration. Is there any way for any way to force Visual Studio to run my compiled code with root privilages?
Personally I come from a long history of C/C++/MFC/Windows programming with no Linux experience at all. I started programming for the Raspberry a year or so ago, when I got one from my son for my birthday. With a C/C++ history it's really not that hard, but you just have to read in a bit because on Linux a lot is the same, and a lot isn't :). Just read and read and in time, you're an experienced Linux programmer! It takes some time I'm afraid.
So, I'm sort of an experienced newbie on this I think :). I found a lot of info on www.die.net (and other Linux man pages). If you search there for a function, say printf(), look at the (3) pages - they explain how you should use 'm.
But although VS2017 does a really good job on this, it's not always going as smooth as you would like. Sometimes it's just necessary to reboot your Raspberry, restart VS and try again. The Linux cross-compiling is quite new in VS and isn't perfect yet.
A few tips:
Use a Raspberry with a fresh Stretch image to start with. Of course after a sudo apt-get update && sudo apt-get -y upgrade.
Set the debugger in Visual Studio to gdb instead of gdbserver. This will prevent certain debugging problems.
Place all your source files (.h and .cpp) in the main folder of your project alongside the main.cpp. If you place them in different directories and then include in your project, you can debug your program but you won't be able to step through.
Start simple and debug your program often when you're developing. Don't add too much code before your next check. In a cross-compile setup there are just a lot more things than normal that can (and will) go wrong.
VS2017 has default Linux include files in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\Linux\include. If you need others, place them in a folder of you own and include the path.
With cross-compiling VS itself doesn't do any compiling, you use it to maintain your project. When the project has to be compiled, all necessary files are copied to the Raspberry and the compiler and linker there are invoked by VS.
On your question how to configure a project for remote debugging: it's probably best to start with the Blink example in Visual Studio 2017 (File, New, Project, Installed, Visual C++, Cross Platform, Linux => Blink). It should run without problems. From there, you can build further on your project.
In the Blink example, they use wiringPiSetupSys() and the remote command gpio export 17 out to run your program without sudo. I've found that changing that to wiringPiSetup() and disabling the remote command, it still is possible to run and debug the program. On the Raspberry I can run the program from it's folder by issuing ./[programname]. If that doesn't work for you, you could run the program by issuing sudo ./[programname].

Cannot run Matlab Mex files on Mac OS X Lion 10.7.5 with Xcode Version 4.3.2

When I am trying to run c++ codes on matlab, I use the following commands:
mex '-setup'
(Then I select the following two options which are:
1: /Applications/MATLAB_R2011a_Student.app/bin/gccopts.sh :
Template Options file for building gcc MEX-files
and
2: /Applications/MATLAB_R2011a_Student.app/bin/mexopts.sh :
Template Options file for building MEX-files via the system ANSI compiler
When I select either one and select a mex file by writing the following:mex mextest1p0.cpp, I get these error messages:
/Applications/MATLAB_R2011a_Student.app/bin/mex: line 1041: g++-4.2: command not found
How can I fix this. Note: I have seen a couple post about this problem and I've tried various solutions, but nothing is working
You need to install a compiler -- namely G++ (the GNU C++ compiler). On OS X, the easiest way to do is this is by installing the XCode Command Line Developer Tools -- open up XCode, go to preferences, choose downloads, and then install the command line tools. If Matlab still can't find G++, you've got a path problem (and tell us about it) -- the solution is then probably in your gccopts script.
You don't actually need to install all of Xcode if you don't want to, I believe. It takes up a lot of hard drive space. You will need (1) an Apple Developer account (free for students). You can then (2) go to the downloads page at developer.apple.com and get just the Command Line Tools download for your version of OS X (the last to these for 10.7 was in March 2013 – it's on the third page of downloads currently). Run the installer for this.
Then you'll need to (3) edit your mexopts.sh file. You'll need to do this if you have Xcode or not. Type the following in your Matlab command window:
edit([matlabroot '/bin/mexopts.sh'])
Make a copy of the file that opens in case you screw up. You'll need to edit the file based on your system and the complier included with the Command Line Tools (you can try getting the versions by running cc -v and c++ -v or similar in Terminal.app). Look for the section related to OS X. You'll need to set the following items (these are from Matlab R2012b and have changed slightly since, but I'm guessing you're using an older version):
CC='llvm-gcc-4.2'
or maybe just
CC='gcc-4.2'
and
SDKROOT='/'
MACOSX_DEPLOYMENT_TARGET='10.7'
If you're using Xcode, the SDKROOT should probably be '/Developer/SDKs/MacOSX10.7.sdk'. For the C++ section just below you'll probably need
CXX='llvm-g++-4.2'
or
CXX='g++-4.2'
(4) Finally, save your edited mexopts.sh file and run the following in the Matlab command window:
mex -setup
Choose your file (press 1 probably) and choose yes ([y]). You should now be able to compile using the mex function.

Anybody tried to compile Go on Windows?, It appears to now support generating PE Format binaries

http://code.google.com/r/hectorchu-go-windows/source/list
If you could compile it successfully, I like to know the procedures of how to.
Assuming you are using Hector's source tree:
Install MinGW and MSYS, along with MSYS Bison and any other tools you think you'll find useful (vim, etc).
Install ed from the GNUWin32 project.
Install Python and Mercurial.
Clone the [hectorchu-go-windows mercurial repository](https://hectorchu-go-windows.googlecode.com/hg/ hectorchu-go-windows) to C:\Go.
Run an MSYS shell (or rxvt). The rest of these are bash commands...
mkdir $HOME/bin
export PATH=$HOME/bin:$PATH
export GOROOT=C:\\Go
export GOARCH=386
export GOOS=mingw
cd /c/Go/src
./all.bash
Correct errors as it spits them out at you, repeat step 10 until it starts building.
It's the same idea as on Linux or MacOS, basically.
However, I still stand by what I said in my comment above: this isn't necessarily going to generate anything that actually works yet. You'd be better served by waiting until this effort has merged into the main Go tree before tackling it, unless your interest is in assisting with the porting effort.
Update: there is now a mostly-functional pre-built windows port available, for those not interested in building the compiler themselves. However, given the rate of change of the project, the lack of formal releases (other than the hg "release" tag), and the possibility of Hector's changes being merged into the main branch soon, keeping up with development via source builds is likely to produce better results over time.
Just FYI, there is seems official one now.
http://code.google.com/p/go-windows/
Hector said he was only able to get as far as being able to compile and run an empty main. See issue 107:
http://code.google.com/p/go/issues/detail?id=107
There is still a lot of work to do in porting that, especially since the code has lots of dependencies on ptrace and syscall, not to mention the different threading models between Linux/BSD and Windows.
Update:
There's a new thread on golang-nuts (started 26.03.2010) with a link to a recent build and some current building instructions (using MinGW+MSYS).