0

I'm trying to build some c programs with gnu make (4.1) through a shell script. I want to use different CFLAGS parameters, which I read from a file. The file contains lines like:

"-O1 -freorder-functions"

I am trying to do something like

while read line
do
    opts="$line"
    make CFLAGS="$opts"
done < $1

but all my attempts end up in the following error:

cc1: error: argument to ‘-O’ should be a non-negative integer, ‘g’, ‘s’ or ‘fast’

So I tried to change the file's contents to only the parameters after -O1 (-freorder-functions), and add the -O1 in the shell script:

opts=$(printf "\"-O%d %s\"", 1, "$line")

(and a lot of other things that seem less sensible) but I still get the same error message.

Hard-coding the parameter to make in the shell script works fine:

make CFLAGS="-O1 -freorder-functions"

Sorry if this is a stupid question, but I can't seem to find any examples of how something like this is done, and I'm new to shell scripting, so I don't really understand how it's treating the variables here. An echo of what I'm attempting to pass to CFLAGS looks okay to me.

4

1 回答 1

2

在文件中的标志周围加上双引号,并且您尽职尽责地正确引用您的 shell 变量,您的make调用最终被

make CFLAGS="\"-O1 -freorder-functions\""

也就是说,在标志中使用双引号。这一直传递给编译器调用,这意味着编译器调用类似于

cc "-O1 -freorder-functions" -c foo.c

...它要求编译器使用优化级别1 -freorder-functions,它抱怨那是无效的。

为了解决这个问题,我会从文件中删除引号。您也可以使用opts=$linewith $lineunquoted,但这并不完全安全。

于 2015-02-22T23:25:00.313 回答