我是否使用 C++ 的 Makefile?正确吗?
Makefile
default:
(!) g++ -Werror -Wunused-variable -Wunused-value -Wunused-function -Wfloat-equal -Wall -ansi -o main -pedantic-errors main.cpp
'/home/HomeName/Desktop/main'
我一直在使用这段代码来编译一个C++文件。这是在 makefile 中使用此代码的好方法吗?此外,我想知道标有(!)的行是否具有正确顺序的编译器选项。
Makefile
default:
(!) g++ -Werror -Wunused-variable -Wunused-value -Wunused-function -Wfloat-equal -Wall -ansi -o main -pedantic-errors main.cpp
'/home/HomeName/Desktop/main'
I have been using this code to compile a C++ file. Is this a good way of using this code in the makefile? Moreover, I wonder if the line marked with (!) has the compiler options in the correct order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我相信您的 makefile 本身不包含“(!)”标记。
你所拥有的“有效”。它使用一组严格的选项来编译程序,然后通过绝对路径名运行它。
然而,它不是很灵活:
make
它只会构建main
main
即使你构建了它一会儿以前,make main
,它将使用一组不同的命令来构建程序,并且不会运行该程序。如果运行程序的行使用当前目录,那就更好了 - 它将允许您更轻松地移动代码。
如果使用make的一些内置功能那就更好了。
C++ 和 C 编译器非常容忍其选项的各种顺序;你所拥有的都可以。
在
make
内部,C++ 编译器通过宏 CXX 来识别;它需要一组由 CXXFLAGS 定义的标志。因此,您可以使用:这允许您运行
make
、make all
和make main
并获取程序main构建。如果您使用前两者中的任何一个,该程序也将运行。仅当源代码自上次编译以来发生更改时,它才会重新编译该程序。如果目录中有其他程序,例如“exercise2.cpp”和“exercise3.cpp”,那么您可以说
makeexercise2exercise3
并且这些程序现在将以大致相同的方式进行编译main
就是这样。如果您确实想在构建程序后运行该程序(从长远来看可能不会这样做),那么您可能会重写编译规则(假设是 GNU Make):
如果您有一个经典的或 POSIX 变体
make
,您可以这样写:使用 '
%
' 的表示法在可用时更加灵活。So, your makefile itself does not contain the '(!)' marking, I believe.
What you have 'works'. It compiles the program with a stringent set of options and then runs it by absolute pathname.
However, it is not very flexible:
main
if you runmake
main
even if you built it a moment agomake main
, it will use a different set of commands to build the program, and it won't run the program.It would be better - it would allow you to move the code more easily - if the line to run the program used the current directory.
And it would be better if you used some of the built-in features of make.
The C++ and C compilers are very tolerant of various orders for their options; what you have is OK.
Inside
make
, the C++ compiler is known by the macro CXX; it takes a set of flags defined by CXXFLAGS. You could, therefore, use:This allows you to run
make
,make all
andmake main
and get the programmain
built. If you use either of the first two, the program will also be run. It will only recompile the program if the source has changed since it was last compiled. If you have other programs in the directory, say 'exercise2.cpp' and 'exercise3.cpp', then you'd be able to saymake exercise2 exercise3
and those would now be compiled in much the same way thatmain
is.If you really wanted to run the program after building it (probably not something you'd do in the long-term), then you'd probably rewrite the compilation rule (assuming GNU Make):
If you have a classic or POSIX variant of
make
, you'd write:The notation using '
%
' is more flexible when it is available.