0

I'm attempting to write a shell script that will run several tests of my C++ program, redefining a macro each time it runs. 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.

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:

#define NUMS 1

I've tried leaving out the '1', but that produces errors as well. It's clear that the script isn't actually redefining the macro. Any thoughts? Thanks!

1
  • It's exactly what the warning says, you're redefining it. You defined it once on the command line, and again inside the file. If your intention is to override it from the command line, surround the definition in your source within an #ifndef NUMS / #endif pair. As to It's clear that the script isn't actually redefining the macro - you're right. The redefinition happened in your #define NUMS 1 source. The command line was seen first. Commented Apr 18, 2016 at 23:43

1 Answer 1

1

By defining the macro on the command-line with -DNUMS=100 you've provided a default value.

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 (i.e. elsewhere in your source, or in this case the command-line.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.