简体   繁体   English

使用Shell脚本中的-D名称编译选项

[英]Using the -D name compilation option from a shell script

I'm attempting to write a shell script that will run several tests of my C++ program, redefining a macro each time it runs. 我试图编写一个外壳脚本,该脚本将运行我的C ++程序的多个测试,每次运行时都重新定义一个宏。 I'm trying to use the -D name preprocessor option (see https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html ), but I'm consistently getting the warning that the macro is being redefined, and then the program executes without redefining it. 我正在尝试使用-D 名称预处理器选项(请参阅https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html ),但是我一直在收到警告,指出正在重新定义宏,然后程序执行而没有重新定义它。

My script is as follows: 我的脚本如下:

#!/bin/bash

#NUMS is number of subdivisions:
for subdiv in 10 100 500 1000
do
    echo NUMS = $subdiv
    g++ -D NUMS=$subdiv project01.cpp -o project01 -lm -fopenmp
    ./project01 >> bezier_results.txt
done

In my C++ file, project01.cpp, I state: 在我的C ++文件project01.cpp中,我声明:

#define NUMS 1

I've tried leaving out the '1', but that produces errors as well. 我试过省略“ 1”,但是也会产生错误。 It's clear that the script isn't actually redefining the macro. 很明显,脚本实际上并没有重新定义宏。 Any thoughts? 有什么想法吗? Thanks! 谢谢!

By defining the macro on the command-line with -DNUMS=100 you've provided a default value. 通过在命令行上使用-DNUMS=100定义宏,您提供了默认值。

You're code is then overriding that default value when you do: 然后,您的代码将在执行以下操作时覆盖该默认值:

#define NUMS 1

The compiler warning is telling you exactly what has happened. 编译器警告会告诉您确切的情况。 Instead consider coding something like this: 而是考虑编写如下代码:

#if !defined NUMS
#define NUMS 1
#endif

Now the compiler will only redefine NUMS as 1 when it hasn't already been defined (ie elsewhere in your source, or in this case the command-line.) 现在,编译器仅在尚未定义NUMS时才将其重新定义为1(即,源代码中的其他位置,或者在这种情况下为命令行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM