【问题标题】:Link an unknown number of object files in a batch using gcc使用 gcc 批量链接未知数量的目标文件
【发布时间】:2016-05-03 17:10:20
【问题描述】:

我在一个目录中有未知数量的目标文件,所有这些文件都名为 compile_to_cX.o ,其中 X 是从 1 到该目录中目标文件数量的值。我想使用 gcc 链接它们,最终结果相当于:

  gcc -Xlinker -no-as-needed compile_to_c1.o ... compile_to_c1N.o -x none
  strip a.out

我尝试使用 fors 和环境变量,但都没有奏效。 提前致谢

【问题讨论】:

  • 目录下除了compile_to_c文件还有.o文件吗?

标签: batch-file gcc batch-processing


【解决方案1】:

dir 在没有/b 选项的情况下运行时,它找到的文件数将显示在底部。由于您可以在文件扩展名上过滤dir,并且除了您想要的文件之外没有其他.o 文件,您可以简单地在dir 输出底部获取文件计数字符串的第一个空格分隔标记。

从那里,您可以使用for /L 循环来构建一个包含您需要编译的所有文件的字符串。

@echo off
setlocal enabledelayedexpansion

:: Get the number of .o files from the dir listing
for /f "tokens=1" %%A in ('dir *.o^|find "File(s)"') do set o_files=%%A

:: Build a string with all of the .o files (up to 512 due to string length limitations)
for /L %%A in (1,1,!o_files!) do set "o_string=!o_string! compile_to_c%%A.o"

:: Run gcc and strip (remove the echos if your strings look correct)
echo gcc -Xlinker -no-as-needed !o_string! -x none
echo strip a.out
pause

【讨论】:

  • 我什么都懂。感谢您的指导SomethingDark,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多