简体   繁体   English

如何清理C ++编译器命令?

[英]How to clean up C++ compiler commands?

I tried searching for a question like this as best as I could, but I simply don't know how exactly to ask the question. 我尝试过尽可能地搜索这样的问题,但是我根本不知道怎么问这个问题。 I apologize if this has been answered before, or if it is a trivial question. 如果您之前已经回答过这个问题,或者这是一个琐碎的问题,我深表歉意。 I am new to the large (and quite intimidating!) world of C++. 我刚接触C ++的广阔世界(并且非常令人生畏!)。 I will try my best in the future. 将来我会尽力而为。

Let me try to explain what I mean: 让我尝试解释一下我的意思:

I am on Linux Ubuntu 14.04. 我在Linux Ubuntu 14.04上。 On Ubuntu, to compile a C++ program I wrote, for example, let's say some file myProgram.cpp, I cd to its directory and type the following into the terminal: 在Ubuntu上,例如,要编译我编写的C ++程序,假设某个文件myProgram.cpp,我cd到其目录,然后在终端中键入以下内容:

g++ myProgram.cpp -o myProgram

This way, I can then type: 这样,我可以输入:

./myProgram

To run myProgram. 运行myProgram。 This was simple and satisfactory -- and I learned this from reading the C++ Primer. 这很简单并且令人满意-我从阅读C ++ Primer中学到了这一点。 After finding myself comfortable coding in C++, I decided to move onto giving myself a project, which involved using Image Magick, specifically, its C++ API: Magick++. 在找到适合自己的C ++编码后,我决定继续给自己一个项目,该项目涉及使用Image Magick,尤其是其C ++ API:Magick ++。 However, now, if I wish to compile something, I have to type this ugly mess: 但是,现在,如果我想编译一些东西,我必须输入以下难看的烂摊子:

g++ `Magick++-config --cxxflags --cppflags` -o testMagick testMagick.cpp `Magick++-config --ldflags --libs`

To make my code compile and execute. 使我的代码编译和执行。 This seems very messy, and when I was trying to see if CImg was any better, it turned out that it needed its own set of arguments like -lm , -lpthread and -lX11 . 这似乎很混乱,当我尝试查看CImg是否更好时,事实证明它需要自己的一组参数,例如-lm-lpthread-lX11 It also turned out that to compile with C++11, I had to write -std=c++0x . 事实证明,要使用C ++ 11进行编译,我必须编写-std=c++0x So my question is: 所以我的问题是:

Is there a way to simplify/clean up my compile statements in the terminal, or do I have to find out and use a specific set of arguments whenever I compile a different kind of program using a different API? 有没有一种方法可以简化/清理终端中的编译语句,还是每当我使用不同的API编译另一种程序时都必须找出并使用一组特定的参数?

In a pinch, you can just make a shell script to execute this for you. 紧要关头,您只需制作一个Shell脚本即可为您执行此操作。 That lets you separate the clutter out: 这使您可以将杂物分开:

#!/bin/bash
IMAPI_OPTS=$(Magick++-config --cxxflags --cppflags)
IMAPI_LIBS=$(Magick++-config --ldflags --libs)

g++ $IMAPI_OPTS -o testMagick testMagick.cpp $IMAPI_LIBS

You could call this file build.sh , and make it executable. 您可以将该文件称为build.sh ,使其可执行。 Then, just typing ./build.sh would do the build the same way each time. 然后, ./build.sh输入./build.sh都会以相同的方式进行构建。

This isn't too far off what a Makefile would look like, however, and using a Makefile may be better for you in the long run. 不过,这与Makefile的外观不太相称,从长远来看,使用Makefile可能对您更好。 The simplest Makefile that behaves like the above, and performs no dependency checks is: 行为与上面类似,并且不执行任何依赖项检查的最简单的Makefile是:

IMAPI_OPTS=`Magick++-config --cxxflags --cppflags`
IMAPI_LIBS=`Magick++-config --ldflags --libs`

all:
        g++ $(IMAPI_OPTS) -o testMagick testMagick.cpp $(IMAPI_LIBS)

In this case, you just save it to the file called Makefile , and run make to do your build. 在这种情况下,您只需将其保存到名为Makefile ,然后运行make进行构建。

If you are interested to know more, try this link: GNU make: Introduction 如果您有兴趣了解更多信息,请尝试以下链接: GNU make:简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM