I am currently trying to set up an "automatic build process" on mac for my C++ "Hello World Project" that uses Cmake & visual studio code.
My current workflow to build in terminal or with vscode's tasks.json is the following, and I find them very tedious to do.
cmake ... (to generate the cmake & make files inside the build folder)
make (inside the build folder to create the application's executable)
./{application's executable}
Thus, I have have looked into the following
vs code's prelaunch task
cmake's add_custom_commands
custom bash script
python scripts
But I'm a bit lost with the above try-outs, and need some helps. My end goal is to automatically build and run the updated code by either pressing F5 in vscode or calling a custom ./{command} in terminal.
Combining everyone's suggestions, the approach I took is to create a task.json that contains the following command-flow
cmd1: cmake to generate the makefiles & cmake files to a build folder
cmd2: make -j 8 to build with multiple cores
cmd1 && cmd2
Then I made two different launches in launch.json. The major difference between them is the prelauch task. One of them is "cmd1&&cmd2", and another one is only "cmd2". That way I can have two hotkeys like F5 and F7 for me to choose. If the folder structure & files weren't added or removed I will hit F7 which only calls the "cmd2" pre-launch task, otherwise F5.
I think a better approach is to either write a bash script or dig deeper in cmakelist.txt which I believe it has the "if statements" that determine when should it do "cmd1 &&cmd2" or just "cmd2" alone. That way, I don't need two hotkeys to build/debug and run in vscode (don't know if it is possible, I haven't try yet.)
If anyone has done it, please comment below or create a new answer.
Thank you all!
You can run
cmake --build
after the first time you configured the build. Optional parameters include the build directory and targets to build.
Related
Analogous setup on *nix:
There is a single project folder with a makefile, and directories for src, build, and target.
In the src directory, I put main.cpp, and other source files (file_0.cpp, file_0.h, file_1.cpp, file_1.h, etc...).
Invoking the makefile (via make, or similar one-line [preferably one-word] script) does nothing fancier than check the src folder if anything is newer than the target, and if so, rebuilds the whole project. (Alternatively, a batch file that just does the compilation regardless of filesystem state would also be "fine"- though I'd prefer a makefile as I could iteratively grow that in complexity as necessary.)
The "build" folder could be used for intermediate build files.
Questions:
What do I need to install? How must I set up my environment? What compilers/tools should I lean into so I'm not fighting the OS? Etc...
My needs for these projects are minimal. The point here is I DON'T want to have to rely on booting up a heavy IDE or similar to putz around with simple C++ projects. Is this achievable in Windows 10?
Problems:
I've tried installing mingw, with msys, and can get cl to produce a .exe, but when I invoke it via a makefile, I get LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'. I've been struggling to find "makefile" resources for windows, as many appear to be straddling different tech stacks (cl vs g++, make vs nmake, etc...)
I have an Eclipse project that builds a library, with my own build script (that basically sets some variables, then calls a Makefile). I have set the Build Configuration to run this script, everything is OK: I can compile by just clicking on the Build button.
Now, I would like to test some parts of this lib. In the same project, I have created a C++ source file, with a little main(). I would like to create a run configuration to execute this small test program, but Eclipse tells me that "the binary does not exist" (of course, since I want to build it...).
What is the solution ?
I'm trying to integrate a legacy borland turbo c++ project into jenkins task and I need to be able to compile the project from command line.
Is there any way to get the compiler CLI information from the project so that I could make a batch file that compiles it?
SO: Windows 7
You can get the commands being invoked by C++ Builder by exporting a makefile for the project.
Under menu goto Project->Export a Makefile.
Once exported open a cmd.exe shell to the generated makefile's location.
Run the makefile with borland's make.exe tool:
make -B -K -n -f"projectMakefileGoesHere"
-B will traverse all the dependencies ignoring age.
-K will keep any temp response files used during the build.
-n do a dry-run printing the commands that would've been called in an actual build.
The link commands will be in the MAKE0xxx.### response file where "x" is a number assigned by make.
For later versions of C++ Builder, the .cbproj project is actually a msbuild project file. You can use msbuild to print the build commands used for the project. eg.
msbuild "project.cbproj" -p:Configuration=Debug -clp:ShowCommandLine -v:n
Unfortunately, msbuild doesn't have a dry-run option so it'll end up building the project. Another idea is to create a simple logging program that replaces bcc32.exe compiler and ilink32.exe linker. With this, you can see exactly what options and switches are being passed to the tools.
When running CMake from the project view in QtCreator ("Run CMake" command), CMake wizard pops up. This is becoming really annoying and since I'm QtCreator newbie, I don't know if there is any remedy for this.
Desired effect would be automatic execution of cmake, without the CMake wizard pestilence.
Yes, this GUI annoying me too.
The only option is to add custom build step in your Project configuration
run app: cmake
params CMakeLists.txt
working folder: %{sourceDir}
this step should be added BEFORE make step.
Once you have created a project in Qt Creator, you do not need to use the "Run CMake" command unless for some reason you think the project files are out of date. Just click build, run, debug etc and it will take care of compiling the files that need to be compiled and invoking CMake as and when necessary.
Underneath it uses Unix Makefiles with Codeblocks in order to generate an XML description of the project. Running CMake from the menu explicitly re-runs CMake and reparses the XML description.
I would like to run a code generator every time my project is built in Visual Studio, even if no source file in the project was changed. Therefore I would like to have a custom build step set up as explained in Visual Studio: Run C++ project Post-Build Event even if project is up-to-date.
How can I create such a build step with CMake?
I think a custom target is what you are looking for: add_custom_target
From the documentation:
Add a target with no output so it will always be built.
Or if you are generating a code file,
https://cmake.org/cmake/help/v2.8.8/cmake.html#command:add_custom_target
can be run POST_BUILD and generate output.
This is afaik not possible with CMake, and is therefore a missing feature for sure.
The answer from Tarydon in the question you refer to, is about setting up precisely what you want - a "Custom Build Step". This means that you still only have your main target (VS Project), with something that looks like a "Post-Build Event" but technically isn't, since Post-Build Events aren't run if the project is up-to-date.
The answer from tpg2114 works, but has one major drawback; it spams your solution with phony projects. In case you have a hundred projects in a solution, having to add another hundred just as post-build wrappers to the first hundred is of course undesirable.
Depending on your situation, it might sometimes be easier to use Post-Build Events and force a rebuild of at least one source file, for the project to actually build and therefore also run your custom command.