Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an alias defined to compile an opencv program which goes like this

alias gcv='g++ -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect'

Then I go on to type

gcv imageshow.cpp

I get this long list of errors

enter image description here

But when I use the same command specified in the alias with the .cpp(source code) file in between the Include and Library files (following the suggestion here), everything works like a charm.

g++ -I/usr/local/include/opencv imageshow.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui

Now typing the long command, which gets even bigger as complexity increases, every time is getting difficult, please help me in getting the alias to work or suggest any other alternative.

share|improve this question
try g++ imageshow.cpp (pkg-config --cflags --libs opencv) also try to add a white space at the end of your alias string. – user827992 Jul 30 '12 at 17:51
No luck with either.. I'm Getting errors.. g++: error: pkg-config -libs opencv: No such file or directory. – Bhargav Jul 30 '12 at 18:05
The space after the alias makes no difference. – Bhargav Jul 30 '12 at 18:08
just try to try only pkg-config --cflags --libs opencv but you appear to missing the necessary packages to develop with opencv, install those packages with sudo apt-get install libopencv-dev and after that run sudo ldconfig – user827992 Jul 30 '12 at 18:11
Every other package is present.. I get a long list on executing the command. – Bhargav Jul 30 '12 at 18:21
up vote 3 down vote accepted

Try this without using aliases:

g++ imageshow.cpp -o imageshow `pkg-config --cflags --libs opencv` 
share|improve this answer
Oh crap.. all this while every other place I saw this command I thought that was a "single quote".. When I saw u stressing on the same thing once again.. I copied and pasted it and took off time and realize that it was "Tilde".. Thanks a TON :D – Bhargav Jul 30 '12 at 18:47
if you are confused by this you can always use an equivalent version which is g++ imageshow.cpp -o imageshow $(pkg-config --cflags --libs opencv) – user827992 Jul 30 '12 at 18:49
I'd say change the backticks to $(...) in the answer. – geirha Jul 31 '12 at 13:38

Why not use a simple script instead of the alias?

file gcv in ~/bin/

g++ $* -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect

Alternatively if you're using zsh you can use a function defined in your .zshrc file:

gcv() { 
    g++ $* -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect;

Automated compiler and linker flags

If you're having linker issues, you can also incorporate pkg-config here:

g++ $* `pkg-config --cflags --libs opencv`

Spaces in filenames

While other posts on similar issues exist, handling spaces in the filenames is not trivial so I thought I'd recap an elegant solution here; specifically I found that making use of the IFS variable helps tremendously.

So making a small addition to end up with

g++ $* `pkg-config --cflags --libs opencv`

solves issues with spaces in filenames too!

share|improve this answer
I'm giving it a shot right now.. I need some clarity on this.. Is it a shell script ?? I name it and put it in ~/bin ?? I am using the default shell so its Bash right ?? – Bhargav Jul 30 '12 at 18:17
@Bhargav - Yes it's a shell script. You need to make it executable using chmod +x gcv (Remove the .sh if you want to invoke using just gcv!). And yes put it in your ~/bin directory, which your .bashrc should already have appended to the $PATH. – jmetz Jul 30 '12 at 18:22
Now.. the last thing u said about the $PATH.. I don't seem to find it in my .bashrc file.. What do I do ?? – Bhargav Jul 30 '12 at 18:36
You can add it yourself; at the end of your ~/.bashrc file add export PATH=$PATH:~/bin - it means that any executable scripts in your bin folder will be callable without having to specify the full path – jmetz Jul 30 '12 at 18:46
Problem: If I have the script in the same folder as the source code its working great else its not.. ERROR: bash: ./gcv: No such file or directory.. – Bhargav Jul 30 '12 at 19:34

The post on creating a shell script wasn't very clear .. This was the simple procedure I followed to create a script of my own and it works even if the filename have spaces in it..

Here's what I did

  • mkdir ~/bin
  • nano ~/bin/gcv
  • sudo chmod u+x ~/bin/gcv
  • Type in the following into the script

        g++ "$@" `pkg-config --libs --cflags opencv`
  • export PATH=$PATH:~/bin

  • Type echo $PATH and make sure u see ~/bin listed in the output u get.
  • Type alais if you see any aliases by the name gcv use the command unalias gcv or else u'll have trouble using gcv as a command to compile the code, May be aliases have higher priority than scripts.
  • Even if my file-names have spaces in them the script won't have an issue.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.