Compling C++ code using command line

后端 未结 2 1060
北恋
北恋 2021-01-27 12:51

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
相关标签:
2条回答
  • 2021-01-27 13:48

    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.

    0 讨论(0)
  • 2021-01-27 13:57

    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"; }
    
    0 讨论(0)
提交回复
热议问题