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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:35:59+00:00 2026-06-07T09:35:59+00:00

I originally asked this question on CMake mailing list: How to configure target or

  • 0

I originally asked this question on CMake mailing list: How to configure target or command to preprocess C file?

I’m porting build configuration based on GNU Autotools to CMake and I have to deal with C preprocessing to generate a file.

The input for preprocessor is SQL file with C preprocessor directives used, like #include "another.sql", etc.

Currently, Makefile uses the following rule to generate plain SQL file as output:

myfile.sql: myfile.sql.in.c
    cpp -I../common $< | grep -v '^#' > $@

So, the myfile.sql is meant to be one of products of the build process, similar to share libraries or executables.

What CMake tools should I use to achieve the same effect?

It’s unclear to me if I should use add_custom_command, add_custom_target or combine both.

Obviously, I’m looking for a portable solution that would work at least with GNU GCC and Visual Studio toolsets. I presume I will have to define platform-specific custom commands, one for cpp preprocessor, one for cl.exe /P.

Or, does CMake provide any kind of abstraction for C preprocessor?

I scanned the archives, but I only found preprocessing of fortran files or solutions based on make capabilities: make myfile.i
So, it’s not quite what I’m looking for.

UPDATE: Added answer based on solution received from Petr Kmoch on CMake mailing list.

  • 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-06-07T09:36:01+00:00Added an answer on June 7, 2026 at 9:36 am

    To take advantage of CMake’ make myfile.i feature, you can do this:

    add_library(sql_cpp_target EXCLUDE_FROM_ALL myfile.sql.in.c)
    

    Now running make myfile.sql.in.c.i will produce preprocessed source for you, using defined CMAKE_C_FLAGS. It might be possible to change output name and dir for preprocessed file.

    At any rate, you’d need to wrap these make invocations into add_custom_target(ALL ...) to make CMake run them during build.

    Use CMAKE_MAKE_PROGRAM variable in targets definitions.

    If you want to abstract from build tool, you can call cmake itself to build a target for you. Use ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target targetname when defining custom target.


    Alternatively, you can just add_custom_command() which runs specified compiler to preprocess files and put them at the appropriate place. But this seems to be less flexible, IMO.

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

Sidebar

Related Questions

I originally asked this question on the Python capi-sig list: How to pass arguments
Note: I originally asked this question about an hour ago but only recently realized
This question was originally asked for Android 1.6. I am working on photos options
This question was originally asked in askubuntu.com but got no attention so I think
UPDATE (5 July 2013): I've learned much since I originally asked this question. In
This question was originally asked for the objective-c programming language. At the time of
I asked a question about this that was originally answered here . Originally, I
I originally asked this question on cstheory.stackexchange.com but was suggested to move it to
I asked this question originally on SuperUser , but it looks like I'm much
I originally asked this question on RefactorMyCode , but got no responses there... Basically

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.