Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

3 Answers 3

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
2  
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. mywiki.wooledge.org/BashFAQ/082 –  geirha Jul 31 '12 at 13:38

Why not use a simple script instead of the alias?

file gcv in ~/bin/

#/bin/sh 
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:

#/bin/sh 
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

#/bin/sh 
IFS=$';'
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 gcv.sh and put it in ~/bin ?? I am using the default shell so its Bash right ?? –  Bhargav Jul 30 '12 at 18:17
1  
@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
1  
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

        #!/bin/bash   
        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

 
discard

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.