开发者

Using makefile variables in C

I need to read a file in a C program and I do not want to hardcode the path to that file. I want 开发者_如何学JAVAto provide that path as a Make variable and then use it in 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.

How can I do that?


This probably should be done with a command line parameter but, if you must do it within the makefile, you can use the following:

$ 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;
}

The -D specifies a compile-time #define which sets MYSTRING to "hello".

Then, when you use MYSTRING in the code, it's turned into the string. In that sample code, I simply pass it to printf but you could equally well pass it to fopen as per your requirement.

When you run that executable, the output is:

[hello]

This is little different to simply hard-coding the value in the source code - you will have to recompile if you ever want the string to change (which is why I suggested a command line parameter in the first paragraph).


You'd want to handle this via string concatenation:

makefile:

PATH = "/usr/bin/"

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

Then in your C file you'd have something like:

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

The compiler will concatenate the strings together into a single string during preprocessing.


If you are gcc or, any similar compiler, you can use the -D flag, documented inside the manpage.

To give a quick overview, you can do gcc -DSYMBOL=1, and this would result in the compiler adding this to the code:

#define SYMBOL 1

So, in your makefile, you can set a make variable, and then pass it to the gcc command line options.


I am beginner to makefile, and I finally got a better way to pass variables from makefile to #define, and won't be affect by Escape.

##1. pass a string

you can use following code

-D STRING_SAMPLE='"string sample"'

is equal to

#define STRING_SAMPLE "string sample"

##2. pass a int or char

you can use following code

-D CHAR_SAMPLE="'\n'"

is equal to

#define CHAR_SAMPLE '\n'

##3. SampleCode

# 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

and 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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜