0

A am using below command to compile my c++ code and which is using OpenCV libraries and my command is just like

    opencv main.cpp -o binary_name

where opencv is an alias command like

alias opencv="g++ `pkg-config --cflags opencv`  `pkg-config --libs opencv`"

but if I forget the "-o binary_name" the command delete my source file. Why this happening....?

What modification should I made on the above alias command to compile my source file like

   opencv main.cpp binary_name  

Thanks in advance.......

Haris
  • 13,645
  • 12
  • 90
  • 121

2 Answers2

1

You can use a function instead of an alias, and use arguments:

function opencv() { g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` "$1" -o "$2"; }
ezod
  • 7,261
  • 2
  • 24
  • 34
1

The order of arguments to gcc is important, source or object files should be given before libraries, and libraries should be invoked with higher level libraries before the lower level libraries they are using.

So you should compile with

    g++ -Wall -g $(pkg-config --cflags opencv) main.cpp \
              $(pkg-config --libs opencv) -o binaryprog

But you really should use a Makefile, or at least have a shellscript.

Don't forget the -Wall option to get all warnings. Improve your code till no warnings are given by the compiler. Use the -g option to get debugging information, to be able to use gdb ./binaryprog to debug your program.

Once your program is debugged, replace -g by -O3 (or perhaps by -O2 -g) to ask GCC to optimize the generated code.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks for reply.Is it possible to alias the above command to a single command so that I can compile my source file by just typing "alias_command source.cpp binary". – Haris Nov 05 '12 at 08:08
  • 1
    You could make a shell function or script to do that, but I don't recommend doing that. Better learn to use `make` or some other builder (because quite soon you'll want to split your code into several translation units). – Basile Starynkevitch Nov 05 '12 at 08:10
  • So for different source code I need to use different make file. My question is can I use a single command instead of the above even if it is not recommended command. Or is it possible using make file. – Haris Nov 05 '12 at 08:24
  • 1
    Not at all, you could have a single `Makefile` handling all your programs in the same directory. – Basile Starynkevitch Nov 05 '12 at 08:27
  • Thanks. I am not well familiarized with the Makefile anyway I will try with the Makefile as you suggested. – Haris Nov 05 '12 at 09:46