-
Notifications
You must be signed in to change notification settings - Fork 8
Linux development environment
To write this part, my initial intention after installing a fresh Ubuntu distribution was to set up any well known IDE and click around until I could compile, debug and run the project. Boy, was I not naive! Several days later, I have had discarded Clion because it is a paying application, I had unsuccessfully tried Code::Blocks, Eclipse, and lost my patience before trying Qt Builder -- yet, I have not been able to find an acceptable IDE to just do the basic stuff with reasonable simplicity. And then I found this Stack Oveflow question «C++ IDE for Linux?» where accepted and most voted answer states that «UNIX is an IDE. All of it».
Let's accept that in Linux we are not going to get a fancy IDE and content ourselves with a text editor. Two popular choices are:
- Sublime Text
- Real hardcore geeks can configure Vim to be an IDE.
The tool to debug code is gdb. It seems bleak, but you can actually do quite a job with it. Have a look on those videos:
- C Programming in Linux Tutorial #056 - GDB debugger (1/2)
- Quick Intro to gdb
- CppCon 2015: Greg Law " Give me 15 minutes & I'll change your view of GDB"
- Hitchhikers guide to the gdb
In the first step, a little bit below, I will give short specific instructions for compiling with debug symbols, and debug the programs with gdb.
This is the official package manager of most Linux distribution. Most popular applications are distributed through it and it is most usually pre-installed.
In a linux distribution, there is the highest chance that pkg-config is already installed. Anyway, you can give it a try; should it be already installed it will just tell you:
sudo apt-get update
sudo apt-get install pkg-config
You will need it to compile OpenCV, so you better install it now:
sudo apt-get install cmake
As a prerequisite, you need to have the following libraries from apt-get:
# required:
sudo apt-get install \
libgtk-3-dev \
pkg-config \
libavcodec-dev \
libavformat-dev \
libswscale-dev
# Optional:
sudo apt-get install \
python-dev \
python-numpy \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libdc1394-22-dev
Once you've installed all prerequisites, fetch the sources of the latest release of OpenCV from the official repository at github, and unzip it:
wget https://github.com/opencv/opencv/archive/4.0.1.zip
unzip 4.0.1.zip
Prepare and compile:
cd opencv-4.0.1
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j2 # Number of processors. Don't use more that you computer has.
Go for a walk; this takes ages. If process breaks, you can launch again by just retyping:
make -j2
When compilation is done, complete the installation:
sudo make install
sudo ldconfig
To check if the library is available as a dependency:
pkg-config --list-all | grep opencv
> opencv OpenCV - Open Source Computer Vision Library
You can now delete the sources folder; you don't need it any more:
cd ..
cd ..
rm -rf opencv-3.4.1
rm 3.4.1.zip
Gtkmm is the Gtk for C++:
sudo apt-get install libgtkmm-3.0-dev
To check if the library is available as a dependency:
pkg-config --list-all | grep gtkmm
gtkmm-3.0 gtkmm - C++ binding for the GTK+ toolkit
To install Git:
sudo apt-get install git
And to verify that Git is present:
git --version
> git version 2.7.4
Clone the example project, configure it with debug symbols, and build it:
cd go-to-your-working-folder
git clone https://github.com/cpp-tutorial/raspberry-cpp-gtk-opencv.git
cd raspberry-cpp-gtk-opencv
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ../src
make
To debug with gdb, assuming that you're still in build
folder:
gdb ./rascapp
[Now you're in gdb]
b main-window.cpp:10 # Place a break-point on line 10 of this file.
run # Run the program. It will stop at the break point.
where # It will show the stack trace
list # It will show some context.
print width # Displays the value of this variable
n # Step over
s # Step into
c # To continue the program
q # Quit gdb
- http://www.alexeyshmalko.com/2014/using-vim-as-c-cpp-ide/
- https://github.com/Valloric/YouCompleteMe#linux-64-bit
- https://github.com/scrooloose/nerdtree
- https://github.com/octol/vim-cpp-enhanced-highlight
- https://github.com/thinca/vim-localrc
- https://vimawesome.com/plugin/conque-gdb
For example:
- Use
set exrc
to make Vim to load the.vimrc
configuration file from the current folder. - Use
set secure
(see https://vi.stackexchange.com/questions/5055/why-is-set-exrc-dangerous)
In the src
folder, create a .vimrc
file. This will be like a workspace configuration. In it, place:
set path+=src/**10
autocmd vimenter * NERDTreeVCS
To open include file:
- gf : Open it.
- C-W f : Open it in a separate tab.
- C-W v : Open current file in a vertical split. Then you can gf to jump to the include file.
To go back after opening an include file:
- gf : Open an include file.
- Ctrl + O : Brings you back to the previous file.
- Ctrl + I : If you're back from a file, then jumps into the next file.
- The mnemonic would be O = OUT, I = IN => Ctrl - O brings you out, Ctrl brings you in. If every jump is like going through a door, that is. (comment by kronn in https://stackoverflow.com/questions/133626/how-do-you-return-from-gf-in-vim)
Create a symlink from the src
folder to the compile_commands.json
:
ln -s build/compile_commands.json compile_commands.json
To set up formatting:
- Read this: https://www.cs.ubc.ca/~goyal/tips/vim-advanced/chapter04.txt
- In
.vimrc
, add following elements:
set path+=src/**10 " For 'gf', looks in current folder, and up to 10 level of sub-sub-folders
set hidden " For 'gf', let's you go to next file without requiring to save current.
set cindent shiftwidth=4 " In C/C++, size of the tabulation increase each time you have a {
set tabstop=4 " In all files, size of the Tab
set expandtab " Use spaces instead of Tab
autocmd vimenter * NERDTreeVCS " Automatically opens NERDTree
To compile and run using vim and conque:
- Edit files as you wish.
- When you want to compile, or perform any other task:
- Ctrl Z - Pauses Vim, and you're back to the command line.
- Do whatever, but
make
-
fg
command reopens Vim at the exact point where you left it. - See more ways here: https://stackoverflow.com/questions/1236563/how-do-i-run-a-terminal-inside-of-vim
- From Vim, type:
-
:ConqueGDB build/rascapp
- The path may vary
-
- From Gdb:
- Here you have some commands: https://www.cs.rochester.edu/~nelson/courses/csc_173/review/gdb.html
- Like any other Vim window,
[I]
let's you type, and[esc]
gets you back in Vim command mode.
This was driving me nuts. Like the poor Davidea explains in his post, https://stackoverflow.com/questions/13122480/core-dump-apport-no-crash-report, there was something wrong in my application that produced a segmentation fault, the core was allegedly dumped, but nowhere to be found.
See here about configuring core dumping:
Verify that user limits for core:
ulimit -c
> 0
ulimit -c unlimited
ulimit -c
> unlimited
If you get an error like the following:
[ 95%] Building CXX object modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_stitch.cpp.o
c++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
modules/stitching/CMakeFiles/opencv_perf_stitching.dir/build.make:62: recipe for target 'modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_stitch.cpp.o' failed
make[2]: *** [modules/stitching/CMakeFiles/opencv_perf_stitching.dir/perf/opencl/perf_stitch.cpp.o] Error 4
CMakeFiles/Makefile2:4438: recipe for target 'modules/stitching/CMakeFiles/opencv_perf_stitching.dir/all' failed
make[1]: *** [modules/stitching/CMakeFiles/opencv_perf_stitching.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
This is usually a problem with not having enough memory. If, like me, you're using a virtual Linux machine, then the solution is simply increasing the available memory. If not, then try not having multi-threaded compilation (don't use make -j
option).
Se more about this issue: https://github.com/opencv/opencv/issues/8552