在Windows

时间:2016-11-18 12:36:06

标签: c gcc linker timestamp symbols

我尝试在Windows上使用链接符号作为gcc的时间戳:

gcc.exe -DTIMESTAMP=$(shell "C:/mingw/msys/1.0/bin/date.exe +%s") -E helloworld.c -o test.E

但是我得到了输出:

  

gcc.exe:错误:C:/mingw/msys/1.0/bin/date.exe + s):没有这样的文件或   目录

" C:/mingw/msys/1.0/bin/date.exe + s"在命令行上工作......

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这里有很多问题:

  1. $()不是有效的命令行语法,它是bash功能。您需要call命令或cmd /C
  2. shell不是命令,批处理文件或二进制文件。如果您有上面的子shell,则没有必要。
  3. 百分号必须使用另一个百分号%%
  4. 进行转义

    以下是在命令行(而不是Powershell)中执行此操作的方法:

    REM Store the output of date.exe to a tempfile
    C:/mingw/msys/1.0/bin/date.exe +%%s > tmpfile
    
    REM Read the file into a variable
    set /p formatteddate=<tmpfile
    
    REM Invoke gcc.exe with the variable
    gcc.exe -DTIMESTAMP=%formatteddate% ...
    

答案 1 :(得分:0)

所有C99编译器(好吧,更像是所有的C编译器) - 肯定包括GCC和Visual C ++,以及你可能会使用的任何常见的C编译器已经提供了宏__DATE__ and __TIME__;有没有理由你不使用它们?

它们扩展为字符串文字,引用预处理器的运行日期和时间。 __DATE__扩展为"Mmm Dd YYYY",例如"Nov 1 2016"; __TIME__扩展为"hh:mm:ss",例如"23:59:59"

如果要在目标文件中使用编译时间戳,可以使用例如

const char timestamp[] = __DATE__ " " __TIME__ ;

在您的C代码中。

当您需要跨更大项目的共享宏时,通过多次调用编译器(例如,在Makefile等中),生成包含在所有编译命令中的头文件。例如,

rm -f timestamp.h
date '+#define TIMESTAMP "%Y-%m-%d %H:%M:%S"' > timestamp.h

并将标记-imacros timestamp.h添加到GCC选项中。