Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:28:02+00:00 2026-05-26T14:28:02+00:00

As the topic says, I want to be able to run a specific command

  • 0

As the topic says, I want to be able to run a specific command during build, and have its output be the definition of a preprocessor macro.

Right now, I have several user-defined variables (in project.pbxproj) and I am able to use their values to fill macro definitions, as follows:

GCC_PREPROCESSOR_DEFINITIONS = (
“STRINGIFY(x)=@#x”,
“_MACRO=STRINGIFY(${MACRO})”,
);
MACRO = foo;

I can set MACRO to a different value per scheme (e.g Debug vs. Release) which is very useful. But, I cannot figure out how to set it by running a command.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:28:03+00:00Added an answer on May 26, 2026 at 2:28 pm

    I can think of 3 options:

    Environment variable: If you build from command line you can export a variable (export ENVMACRO=superfoo) before invoking the build command and use it in an Xcode configuration file OTHER_CFLAGS=-DMACRO=$(ENVMACRO). You need to configure the target with the .xcconfig file.

    Run Script Build Phase: Custom script that generates a header file.

    MACROVALUE=$(run-command-to-obtain-value)
    echo "#define MACRO ($MACROVALUE)" > $(SRCROOT)/path/to/header.h
    

    In my tests you need an empty header file to be able to compile properly. There are other options like modifying an existing file using sed or any other command.

    Custom Build Rule: Custom script that process an input file and creates an output file. Similar to Run Script build phase but slightly better because it will run the script only when the input file has been modified. For example, create a .macro file and process it to update a header file.

    In Xcode > Target > Build rules, add new custom rule.

    Process: *.macro

    Custom script:

    HEADER="${SRCROOT}/Config.h"
    cd ${SRCROOT}
    echo "// Do not edit" > $HEADER
    cat "${INPUT_FILE_PATH}" | while read line
    do
        macro="`echo $line | cut -d= -f1`"
        cmd="`echo $line | cut -d= -f2-`"
        value=$($cmd)
        echo "#define ${macro} @\"${value}\"" >> $HEADER
    done
    echo "// Updated on "`date` >> $HEADER
    

    Output files: $(SRCROOT)/Project.h

    Project.macro contains pairs MACRO=one-liner-command. Like these two non-sense examples:

    COMMIT=git log --pretty=format:%h -n 1
    BUILDYEAR=date +%Y
    

    Generated file will look like:

    // Do not edit
    #define COMMIT @"c486178"
    #define BUILDYEAR @"2011"
    // Updated on Sat Oct 29 14:40:41 CEST 2011
    

    Each time Project.macro changes, the generated header will be updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just as the title says. I want to use a preprocessor macro in the
I guess the topic says it. I have tried googling this, but havent gotten
I have a problem as the topic name says: Lets say i have gallery
I want to run the jms tutorial files in eclipse. I have configured glassfish
As topic says, I want to remove the visability while i hover a list
As the topic says I'm very new to c++, but I have some experience
The topic says the most of it - what is the reason for the
The topic says it all. Using Compact Framework C# I'm tiling (order/sequence is important)
The topic generically says it all. Basically in a situation like this: boost::scoped_array<int> p(new
As topic says, I need to extend the features of a standard Silverlight ComboBox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.