我是否使用 C++ 的 Makefile?正确吗?

发布于 2024-10-18 00:43:13 字数 309 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-10-25 00:43:13

因此,我相信您的 makefile 本身不包含“(!)”标记。

你所拥有的“有效”。它使用一组严格的选项来编译程序,然后通过绝对路径名运行它。

然而,它不是很灵活:

  • 如果你运行 make 它只会构建 main
  • 它总是会构建 main 即使你构建了它一会儿以前,
  • 如果您运行 make main,它将使用一组不同的命令来构建程序,并且不会运行该程序。

如果运行程序的行使用当前目录,那就更好了 - 它将允许您更轻松地移动代码。

如果使用make的一些内置功能那就更好了。

C++ 和 C 编译器非常容忍其选项的各种顺序;你所拥有的都可以。

make 内部,C++ 编译器通过宏 CXX 来识别;它需要一组由 CXXFLAGS 定义的标志。因此,您可以使用:

CXX = g++
CXXFLAGS_W = -Werror -Wunused-variable -Wunused-value -Wunused-function \
             -Wfloat-equal -Wall
CXXFLAGS_M = -ansi  -pedantic-errors
CXXFLAGS   = ${CXXFLAGS_M} ${CXFLAGS_W}

all:    main
        ./main

这允许您运行 makemake allmake main 并获取程序 main构建。如果您使用前两者中的任何一个,该程序也将运行。仅当源代码自上次编译以来发生更改时,它才会重新编译该程序。如果目录中有其他程序,例如“exercise2.cpp”和“exercise3.cpp”,那么您可以说 makeexercise2exercise3 并且这些程序现在将以大致相同的方式进行编译main 就是这样。

如果您确实想在构建程序后运行该程序(从长远来看可能不会这样做),那么您可能会重写编译规则(假设是 GNU Make):

% : %.cpp
        ${CXX} ${CXXFLAGS} -o $@ $*.cpp
        ./$@

如果您有一个经典的或 POSIX 变体make,您可以这样写:

.cpp:
        ${CXX} ${CXXFLAGS} -o $@ $*.cpp
        ./$@

使用 '%' 的表示法在可用时更加灵活。

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:

  • it will only build main if you run make
  • it will always build main even if you built it a moment ago
  • if you run make 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:

CXX = g++
CXXFLAGS_W = -Werror -Wunused-variable -Wunused-value -Wunused-function \
             -Wfloat-equal -Wall
CXXFLAGS_M = -ansi  -pedantic-errors
CXXFLAGS   = ${CXXFLAGS_M} ${CXFLAGS_W}

all:    main
        ./main

This allows you to run make, make all and make main and get the program main 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 say make exercise2 exercise3 and those would now be compiled in much the same way that main 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):

% : %.cpp
        ${CXX} ${CXXFLAGS} -o $@ $*.cpp
        ./$@

If you have a classic or POSIX variant of make, you'd write:

.cpp:
        ${CXX} ${CXXFLAGS} -o $@ $*.cpp
        ./$@

The notation using '%' is more flexible when it is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文