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 386717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:36:12+00:00 2026-05-12T15:36:12+00:00

I would like to pass some options to a compiler. The option would have

  • 0

I would like to pass some options to a compiler.
The option would have to be calculated at compile time – everytime when ‘make’ is invoked, not when ‘cmake’, so execute_process command does not cut it. (does it?)

For instance passing a date to a g++ compiler like that:

g++ prog.cpp -o prog -DDATETIME="17:09:2009,14:25"

But with DATETIME calculated at compile time.

Any idea how to do it in CMake?

Bounty edit:

A least hackish solution will be accepted.

Please note that I want to be able to exectue an arbitrary command at compile time, not only ‘date’.

Edit 2:

It have to work on Linux, Windows (VS), Mingw, Cygwin and OS X.
You can’t assume Ruby, Perl or Python as they are non standard on Windows.
You can assume BOOST, but I guess it’s no use.

The goal is to force cmake to generate Makefile (in case of Linux) that when make is executed, will do the job.

Creating custom *.h file is ok, but it has to be initiated by a Makefile (or equivalent on other OS) by make. The creation of *.h doesn’t have to (and shouldn’t have to) use cmake.

  • 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-12T15:36:12+00:00Added an answer on May 12, 2026 at 3:36 pm

    You’re leaving out some information, such as which platforms you need
    to run this on and if there are any additional tools you can use. If
    you can use Ruby, Perl, of Python, things become much simpler. I’ll
    assume that you want to run on both Unix and Windows pqlatform and
    that there are no extra tools available.

    If you want the output from the command in a preprocessor symbol, the
    easiest way is to generate a header file instead of fiddling around
    with command line parameters. Remember that CMake has a script-mode
    (-P) where it only processes script commands in the file, so you can
    do something like this:

    CMakeLists.txt:

    project(foo)  
    cmake_minimum_required(VERSION 2.6)
    add_executable(foo main.c custom.h)
    include_directories(${CMAKE_CURRENT_BINARY_DIR})  
    add_custom_command(OUTPUT custom.h 
        COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/custom.cmake)
    

    The file “custom.h” is generated at compile time by the command “cmake
    -P custom.cmake”. custom.cmake looks like this:

    execute_process(COMMAND uname -a 
        OUTPUT_VARIABLE _output OUTPUT_STRIP_TRAILING_WHITESPACE)
    file(WRITE custom.h "#define COMPILE_TIME_VALUE \"${_output}\"")
    

    It executes the command (in this case “uname -a”, you’ll replace it
    with whatever command you wish), and puts the output in the variable
    _output, which it then writes to custom.h. Note that this will only
    work if the command outputs a single line. (If you need multiline
    output, you’ll have to write a more complex custom.cmake, depending on
    how you want the multiline data into your program.)

    The main program looks like this:

    #include <stdio.h>
    #include "custom.h"
    int main()
    {
      printf("COMPILE_TIME_VALUE: %s\n", COMPILE_TIME_VALUE);
      return 0;
    }
    

    If you actually want to to calculate compiler options at compile time,
    things become much trickier. For Bourne-shell generators you can just
    insert the command inside backticks. If you get mad while figuring out
    quoting, move all the logic of your command inside a shell-script so
    you only need to put mycommand.sh in your add_definitions():

    if(UNIX)
      add_definitions(`${CMAKE_CURRENT_SOURCE_DIR}/custom-options.sh`)
    endif()
    

    For Windows batch file based generators things are much tricker, and I
    don’t have a good solution. The problem is that the PRE_BUILD commands
    are not executed as part of the same batch file as the actual compiler
    invocation (study the BuildLog.htm for details), so my initial idea
    did not work (generating a custom.bat in a PRE_BUILD step and then do
    “call custom.bat” on it to get a variable set which can later be
    referenced in the compiler command line). If there is an equivalent of
    backticks in batch files, that would solve the problem.

    Hope this gives some ideas and starting points.

    (Now to the inevitable counter-question: what are you really
    trying to do?)

    EDIT: I’m not sure why you don’t want to let CMake be used to generate the header-file. Using ${CMAKE_COMMAND} will expand to the CMake used to generate the Makefiles/.vcproj-files, and since CMake doesn’t really support portable Makefiles/.vcproj-files you will need to rerun CMake on the target machines.

    CMake also has a bunch of utility commands (Run “cmake -E” for a list) for this explicit reason. You can for example do

    add_custom_command(OUTPUT custom.h COMMAND ${CMAKE_COMMAND} -E copy file1.h file2.h)
    

    to copy file1.h to file2.h.

    Anyway, if you don’t want to generate the header-files using CMake, you will either need to invoke separate .bat/.sh scripts to generate the header file, or do it using echo:

    add_custom_command(OUTPUT custom.h COMMAND echo #define SOMETHING 1 > custom.h)
    

    Adjust quoting as needed.

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

Sidebar

Related Questions

I would like to pass some options to Python (version 2.6) every time, not
I would like to pass an argument(s) to a method being defined using define_method,
I would like to pass an x amount of geo-locations to the Google Maps
I would like to pass a reference of a method into another method and
In PL/SQL, I would like to pass in a source schema as a parameter
I would like to have a reference for the pros and cons of using
I have an application A that I would like to be able to invoke
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for

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.