使用命令行来编译C ++代码 [英] Compling C++ code using command line

查看:80
本文介绍了使用命令行来编译C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的命令来编译我的c ++代码,它使用的是OpenCV库,我的命令就像

  opencv main.cpp -o binary_name 

其中opencv是一个别名命令,如

 别名opencv =g ++`pkg-config --cflags opencv``pkg-config --libs opencv`

但如果我忘记了-o binary_name命令,请删除我的源文件。为什么会发生这种情况....?



我应该对上述别名命令进行哪些修改来编译我的源文件,如

  opencv main.cpp binary_name 

预先感谢.......

解决方案

参数的顺序 gcc 很重要,源或目标文件应该在库之前提供,而库应该在它们使用的较低级别库之前使用更高级别库调用。



因此,您应该使用

  g ++ -Wall -g $(pkg-config --cflags opencv)main进行编译。 cpp \ 
$(pkg-config --libs opencv)-o binaryprog

但是你真的应该使用 Makefile ,或者至少有一个shell。



不要忘记 -Wall 选项来获取所有警告。改进你的代码直到编译器没有给出警告。使用 -g 选项获取调试信息,以便能够使用 gdb ./binaryprog 来调试程序。

一旦你的程序被调试,用 -O3 -g $ c>(或者可能是 -O2 -g )来要求GCC优化生成的代码。


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.......

解决方案

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.

这篇关于使用命令行来编译C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆