【问题标题】:Using makefile variables in C在 C 中使用 makefile 变量
【发布时间】:2011-04-14 01:21:06
【问题描述】:

我需要在 C 程序中读取一个文件,并且我不想硬编码该文件的路径。我想将该路径作为 Make 变量提供,然后在 C prog 中使用它。

file name is xyz.txt and I want to do something like this in C prog:
fopen ("PATH/xyz.txt", "r"); 
here PATH is specified in make command that compiles this file.

我该怎么做?

【问题讨论】:

  • 在编译时通过 make 设置 PATH 与硬编码没有什么不同;它仍然是在可执行文件中预定义的。你最好让它接受命令行参数或在运行时从环境变量中读取。
  • @Ken:我相信,它比硬编码更好。该文件的位置因该程序的使用者而异。所以我需要一种不需要修改/更改程序界面的方法。例如,对于 consumer1 - 它的 makefile 设置 PATH = /a/b/c 和对于 comsumer2 - 它的 makefile 设置 PATH = /d/e/f/

标签: c file makefile


【解决方案1】:

这可能应该使用命令行参数完成,但如果您必须在 makefile 中完成,您可以使用以下内容:

$ cat makefile
qq: myprog.c makefile
    gcc -DMYSTRING='"hello"' -o myprog -Wall myprog.c


$ cat myprog.c
#include <stdio.h>

int main(void) {
    printf ("[%s]\n", MYSTRING);
    return 0;
}

-D 指定编译时#define,它将MYSTRING 设置为"hello"

然后,当你在代码中使用MYSTRING 时,它就变成了字符串。在该示例代码中,我只是将其传递给 printf,但您也可以根据您的要求将其传递给 fopen

当你运行那个可执行文件时,输出是:

[hello]

这与简单地硬编码源代码中的值几乎没有什么不同 - 如果您想要更改字符串,则必须重新编译(这就是我在第一段中建议使用命令行参数的原因)。

【讨论】:

  • 酷...非常感谢。另一方面,我想,我可以将 PATH 设置为 Makefile 中的环境变量,并将其与 getenv() 一起使用??
  • @hari - 不,那行不通。 Makefile 确定编译时参数,但getenv() 在运行时起作用。
【解决方案2】:

您希望通过字符串连接来处理这个问题:

制作文件:

PATH = "/usr/bin/"

program: # whatever
    $CC /DPATH=$(PATH)

然后在你的 C 文件中你会有类似的东西:

fopen(PATH "xyz.txt", "r");

编译器会在预处理过程中将字符串连接成一个字符串。

【讨论】:

    【解决方案3】:

    如果您是 gcc 或任何类似的编译器,您可以使用 -D 标志,记录在 inside the manpage.

    为了快速概览,您可以使用gcc -DSYMBOL=1,这将导致编译器将其添加到代码中:

    #define SYMBOL 1
    

    因此,在您的 makefile 中,您可以设置一个 make 变量,然后将其传递给 gcc 命令行选项。

    【讨论】:

      【解决方案4】:

      我是makefile的初学者,终于有更好的方法将变量从makefile传递到#define,并且不会受到Escape的影响。

      ##1。传递一个字符串

      您可以使用以下代码

      -D STRING_SAMPLE='"string sample"'
      

      等于

      #define STRING_SAMPLE "string sample"
      

      ##2。传递一个 int 或 char

      您可以使用以下代码

      -D CHAR_SAMPLE="'\n'"
      

      等于

      #define CHAR_SAMPLE '\n'
      

      ##3。示例代码

      # makefile sample
      CC = gcc
      CCFLAGS = -Wall -O
      
      # run `make test` in terminal
      test:main run clean
      
      run:
          @./main.e
      
      main: 
          @$(CC) $(CCFLAGS) -c main.c -D STRING_SAMPLE='"string sample"' -D CHAR_SAMPLE="'\n'" -o main.o 
          @$(CC)  main.o -o main.e
      
      .PHONY:clean
      clean:
          @rm *.e
          @rm *.o
      

      和main.c

      //sample code in C
      #include "stdio.h"
      #include "string.h"
      int main(void){
          puts("start test char");
          printf("value(hex)==%.2x\n",CHAR_SAMPLE);
          puts("end test char");
          int i;
          puts("start test string");
          for (i=0; i< sizeof(STRING_SAMPLE); i++) {
              printf("value%d(hex)==<%.2x>\n",i,STRING_SAMPLE[i]);
          }
          puts("end test string");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-12
        • 2021-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多