0

I would like to automate the build of CMake using an MSVC C++ script instead of using CMake-gui to generate the build or CMake terminal or using the CMake integrated on MSVC 2017 by right click on the CMakeLists.txt to build it manually. Assume we have a project (name it: initialize) that includes the CMakeLists.txt and initialize.cpp, so my question is how I can convert these commands into a C++ code, assume build_initialize.cpp:

  • mkdir build
  • cd build/
  • cmake ..

So, the requirement of this tiny C++ code is to

  • Set the path to this project
  • Create build folder
  • Run CMake

At the end if I execute build_initialize.exe, the job is just to build the initialize.cpp using CMake. The goal is to test if the build is success or not as a test case within another project that has several test cases.

You may ask, why I didnot include it to the top CMakelists.txt, and then build it from the beginning using CMake. If I am going to do that, I will get an executable file. As a result, by running the ctest of CMake, the initialize.exe will require a pace of hardware. This is not the goal. My goal is just to build it. If I run build_initialize.exe, just repeat the build using CMake without initialize.exe execution.

Sorry, it could be very simple, but I lack the good experience either in C++ or CMake. Two days have been lost without success.

10
  • 1
    You create scripts in scripting languages like Python, Perl etc. C++ is not one. Commented Apr 24, 2019 at 15:17
  • 4
    On Windows consider using the builtin PowerShell scripting ability (or failing that batch files). Commented Apr 24, 2019 at 15:30
  • I expect that your problem is you called system() more than 1 time in your c++ code or did not escape your strings properly. Although theses are just guesses since you did not provide any code. Commented Apr 24, 2019 at 15:53
  • If you are using Git for Windows just use a bash script. There are much easier ways than using C++. Commented Apr 24, 2019 at 17:02
  • 1
    Then show what you have tried and failed. Remember we generally don't write code for you at StackOverflow however we will attempt to fix your broken code. Please edit your question and add a minimal example of your attempt: minimal reproducible example Commented Apr 24, 2019 at 21:05

1 Answer 1

1

Thanks to all of you for the comments. The answer has been given by @Fred. To run cmake from C++ script, simply the system() can be used such as: System(cmake -S path_to_src -B path_to_bld). Useful link: https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284392&answer=1044654269

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.