您如何使编译器行更短?

发布于 2024-07-21 07:44:12 字数 765 浏览 5 评论 0原文

通常,当我与其他人一起处理一个项目时,随着时间的推移,编译器在 Makefile 中获取的库路径和包含路径的数量会变得越来越多。 此外,路径也可能会变得很长。

这是一个例子:

g++ -c -pipe -O2 -Wall -W -DQT_BOOTSTRAPPED -DQT_MOC -DQT_NO_CODECS
-DQT_LITE_UNICODE -DQT_NO_LIBRARY -DQT_NO_STL -DQT_NO_COMPRESS
-DQT_NO_DATASTREAM -DQT_NO_TEXTSTREAM -DQT_NO_TEXTCODEC -DQT_NO_UNICODETABLES
-DQT_NO_THREAD -DQT_NO_REGEXP -DQT_NO_QOBJECT -DQT_NO_SYSTEMLOCALE
-DQT_NO_GEOM_VARIANT -DQT_NO_USING_NAMESPACE -D_LARGEFILE64_SOURCE
-D_LARGEFILE_SOURCE -I../../../mkspecs/qws/linux-generic-g++ -I.
-I../../corelib/arch/generic -I../../../include -I. -I../../../include/QtCore
-I. -I.uic/release-shared -o release-shared/moc.o moc.cpp

我想知道您使用什么样的方法来使编译器行更短,同时仍然为用户提供显示原始行的选项(如果他们稍后确实需要该信息)。

有没有自动执行此操作的工具?

Often when I'm working on a project with others, the amount of library paths and include paths that get sourced by the compiler in the Makefile get more numerous as time goes by. Also the paths can get very long as well.

Here's an example:

g++ -c -pipe -O2 -Wall -W -DQT_BOOTSTRAPPED -DQT_MOC -DQT_NO_CODECS
-DQT_LITE_UNICODE -DQT_NO_LIBRARY -DQT_NO_STL -DQT_NO_COMPRESS
-DQT_NO_DATASTREAM -DQT_NO_TEXTSTREAM -DQT_NO_TEXTCODEC -DQT_NO_UNICODETABLES
-DQT_NO_THREAD -DQT_NO_REGEXP -DQT_NO_QOBJECT -DQT_NO_SYSTEMLOCALE
-DQT_NO_GEOM_VARIANT -DQT_NO_USING_NAMESPACE -D_LARGEFILE64_SOURCE
-D_LARGEFILE_SOURCE -I../../../mkspecs/qws/linux-generic-g++ -I.
-I../../corelib/arch/generic -I../../../include -I. -I../../../include/QtCore
-I. -I.uic/release-shared -o release-shared/moc.o moc.cpp

I'm wondering what kind of recipes you use to make compiler lines much shorter, while still giving the user the option to display the raw lines if they really need that information later.

Are there tools that do this automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

煞人兵器 2024-07-28 07:44:12

您不仅可以使编译器输出更短,还可以对其进行颜色编码,并添加详细标志。 输出将如下所示:

替代文本 http://img526.imageshack.us/img526/9572/sconsf。 以下是操作

方法(从 SCons Wiki 窃取的颜色主题):

import os,sys
colors = {}
colors['cyan']   = '\033[96m'
colors['purple'] = '\033[95m'
colors['blue']   = '\033[94m'
colors['green']  = '\033[92m'
colors['yellow'] = '\033[93m'
colors['red']    = '\033[91m'
colors['end']    = '\033[0m'

#If the output is not a terminal, remove the colors
if not sys.stdout.isatty():
   for key, value in colors.iteritems():
      colors[key] = ''

compile_source_message = '%s\nCompiling %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

compile_shared_source_message = '%s\nCompiling shared %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

link_program_message = '%s\nLinking Program %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

link_library_message = '%s\nLinking Static Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

ranlib_library_message = '%s\nRanlib Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

link_shared_library_message = '%s\nLinking Shared Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

java_compile_source_message = '%s\nCompiling %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

java_library_message = '%s\nCreating Java Archive %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

env = Environment()
AddOption("--verbose",action="store_true", dest="verbose_flag",default=False,help="verbose output")
if not GetOption("verbose_flag"):
  env["CXXCOMSTR"] = compile_source_message,
  env["CCCOMSTR"] = compile_source_message,
  env["SHCCCOMSTR"] = compile_shared_source_message,
  env["SHCXXCOMSTR"] = compile_shared_source_message,
  env["ARCOMSTR"] = link_library_message,
  env["RANLIBCOMSTR"] = ranlib_library_message,
  env["SHLINKCOMSTR"] = link_shared_library_message,
  env["LINKCOMSTR"] = link_program_message,
  env["JARCOMSTR"] = java_library_message,
  env["JAVACCOMSTR"] = java_compile_source_message,

使用“scons --verbose”编译以查看正常的庞大 gcc 输出。

Not only can you make your compiler output shorter, you can colorcode it, and add a verbose flag. The output will look something like this:

alt text http://img526.imageshack.us/img526/9572/sconsf.png

Here's how (color theme stolen from the SCons Wiki):

import os,sys
colors = {}
colors['cyan']   = '\033[96m'
colors['purple'] = '\033[95m'
colors['blue']   = '\033[94m'
colors['green']  = '\033[92m'
colors['yellow'] = '\033[93m'
colors['red']    = '\033[91m'
colors['end']    = '\033[0m'

#If the output is not a terminal, remove the colors
if not sys.stdout.isatty():
   for key, value in colors.iteritems():
      colors[key] = ''

compile_source_message = '%s\nCompiling %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

compile_shared_source_message = '%s\nCompiling shared %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

link_program_message = '%s\nLinking Program %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

link_library_message = '%s\nLinking Static Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

ranlib_library_message = '%s\nRanlib Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

link_shared_library_message = '%s\nLinking Shared Library %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

java_compile_source_message = '%s\nCompiling %s==> %s$SOURCE%s' % \
   (colors['blue'], colors['purple'], colors['yellow'], colors['end'])

java_library_message = '%s\nCreating Java Archive %s==> %s$TARGET%s' % \
   (colors['red'], colors['purple'], colors['yellow'], colors['end'])

env = Environment()
AddOption("--verbose",action="store_true", dest="verbose_flag",default=False,help="verbose output")
if not GetOption("verbose_flag"):
  env["CXXCOMSTR"] = compile_source_message,
  env["CCCOMSTR"] = compile_source_message,
  env["SHCCCOMSTR"] = compile_shared_source_message,
  env["SHCXXCOMSTR"] = compile_shared_source_message,
  env["ARCOMSTR"] = link_library_message,
  env["RANLIBCOMSTR"] = ranlib_library_message,
  env["SHLINKCOMSTR"] = link_shared_library_message,
  env["LINKCOMSTR"] = link_program_message,
  env["JARCOMSTR"] = java_library_message,
  env["JAVACCOMSTR"] = java_compile_source_message,

Compile with "scons --verbose" to see the normal bulky gcc output.

面犯桃花 2024-07-28 07:44:12

如果主要是“make”期间喷出的大行造成了烦恼,您还可以更改 Makefile 以不回显编译器行,而是使用类似以下内容的内容:

     .cpp.o:
          @echo $(CC) 
lt;
          @$(CC) $(FLAGS) -c -o $@ 
lt;

“@”抑制命令行的回显

If it's mostly the spewing of huge lines during 'make' that causes the annoyance, you can also change your Makefile to not echo the compiler line, but to instead have something like:

     .cpp.o:
          @echo $(CC) 
lt;
          @$(CC) $(FLAGS) -c -o $@ 
lt;

The '@' suppresses the echo of the command line

笑梦风尘 2024-07-28 07:44:12

使用环境变量怎么样?

export LONGPATH=/usr/local/projects/include/foo/system/v1
gcc foo.c -o foo -I$LONGPATH

对于更复杂的场景,可以使用包装器来调用编译器,以便实际的命令及其参数仅在出现错误或警告时显示。
例如,使用 cmake,许多传统输出已经被大幅缩减。

同样,可以将 spec 文件与 gcc 一起使用

How about using environment variables?

export LONGPATH=/usr/local/projects/include/foo/system/v1
gcc foo.c -o foo -I$LONGPATH

For more complex scenarios, one can use wrappers to invoke the compiler, so that the actual commands and their parameters only show up on errors or warnings.
Using cmake for example, much of the conventional output is already downstripped heavily.

Similarly, there's the possibility to use spec files with gcc.

路弥 2024-07-28 07:44:12

在 scons 中,我将换行符插入到命令生成器中,以使长命令更具可读性:

例如,

tool \
    -opt1 bar1 \
    -opt2 bar2 \
    -opt3 bar3 \
    -opt4 bar4

然后在构造命令字符串时创建一个本地方法以减少混乱。

cmds = []
def l(line,indent=1):
    cmds.append(indent*'    '+line)
l('tool',0)
l('-opt1 bar1')
l('-opt2 bar2')
l('-opt3 bar3')
l('-opt4 bar4')

return '\\\n'.join(cmds)

In scons, I insert newlines into my command generators to make long commands more readable:

e.g.

tool \
    -opt1 bar1 \
    -opt2 bar2 \
    -opt3 bar3 \
    -opt4 bar4

then I create a local method when constructing the command string to reduce the clutter.

cmds = []
def l(line,indent=1):
    cmds.append(indent*'    '+line)
l('tool',0)
l('-opt1 bar1')
l('-opt2 bar2')
l('-opt3 bar3')
l('-opt4 bar4')

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