CMake: specifying build toolchain - makefile

Very new to CMake, and so far I'm finding it to be extremely helpful. I have a set of custom libraries that I would like to build for multiple platforms using cross-compilation. The toolchains are installed, and I can hand-create the Makefiles that I need to do so, but I would like to be able to make use of CMake.
Is there a way to tell cmake which toolchain to use, either at the command line or in the CMakeLists.txt file?

Have a look here: basically, you define a "toolchain file" that sets things like the system name, paths to compilers and so on. You then call cmake like so:
cmake /path/to/src -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/foo-bar-baz.cmake

To customize the build settings so you don't have to specify the parameter every time:
For Visual Studio - here
For Vusual Studio Code :
Install the Cmake Tools extension if haven't done so already.
In the .vscode/settings.json file set the parameter cmake.configureArgs. You can also set it from Settings -> CMake Tools configuration -> Add Item. Mine looks like this:
{
"cmake.configureArgs": [
"-DCMAKE_TOOLCHAIN_FILE=C:/Users/.../vcpkg/scripts/buildsystems/vcpkg.cmake"
]
}

Related

With vcpkg portfile, how to insert regular CMake command?

I'm trying to customize a lib installed through vcpkg with regular CMake commands.
The only thing I need to tweak is to enable a preprocessor, e.g., BUILD_WITH_THIS_OPTINAL_FEATURE.
With Visual Studio, it's pretty straightforward: Just add it to the C/C++ > Preprocessor property of the project. But with vcpkg, it's unclear how to mix it with its own functions.
Say if I have a portfile ready under
E:\_dev\vcpkg\ports\mylib\portfile.cmake
I'd really love to let it know that I want to
add_compile_definitions(BUILD_WITH_THIS_OPTINAL_FEATURE)
But doing so directly in portfile.cmake gives me
CMake Error at ports/mylib/portfile.cmake:38 (add_compile_definitions):
Unknown CMake command "add_compile_definitions".
Anyone knows how?
Solved it myself.
I just need to add the following to vcpkg_configure_cmake's OPTIONS input arg
-DBUILD_WITH_THIS_OPTINAL_FEATURE=ON

How to setup meson with Qt creator

I normally use Qt creator with cmake to program C++ projects. Lately I read quite a bit about meson and it's simplicity and I like to test it. This example explains how to setup meson.
When using meson, I like however to still use Qt creators shortcuts for building (ctrl + B) or running (ctrl + R). How can I configure Qt creator to build a meson project, when I'm using a "generic project"?
Meson is currently not directly supported by Qt Creator. There is a bug report requesting that: https://bugreports.qt.io/browse/QTCREATORBUG-18117 and I am considering to actually implement that.
For the time being I use meson via the "Generic Project". Go to "New File or Project", "Import Project" and there "Import Existing Project". That gets you a dialog where you can select the files that your project consists of.
After that is done you will need to edit "projectname.includes" and add the include directories (one per line) into that file. Then you need to edit "projectname.config" and add defines (one per line) there.
Finally you will need to edit the build configuration and call ninja instead of make there.
With that it works reasonably well for my small project.
Until the QtCreator supports directly meson.build project files, I find this python2 script useful to create QtCreator generic project files: https://github.com/mbitsnbites/meson2ide
with meson and ninja in your PATH, this should work:
$ meson builddir
$ python2 meson2ide.py builddir
this generates a .creator project file in builddir (if you get an error about "mesonintrospect" not found, try this PR: https://github.com/mbitsnbites/meson2ide/pull/1)
To make CTRL+B work properly, In QtCreator build settings, remove the make build step and add a custom build step with the path to the ninja executable, and add the command line arguments
3>&1 1>&2 2>&3
Those redirect allow QtCreator to capture build errors in the "issue" panel.

change the paths for boost libs in Blender's CMake config

I need help on how to compile blender using the boost I have installed on my machine. I have to change the paths for boost libs in Blender's CMake config but I have no idea on how to do that. Is it one of the many cmake text files that I have to manually alterer?
this is the directions I'm following
https://wiki.blender.org/index.php/Dev:Doc/Building_Blender/Mac
thanks,
pascal
You don't need to edit any of blender's CMakeLists.txt files, the boost locations are part of the configuration of the build that you can set and alter.
As you are on a Mac and may be unfamiliar with using CLI tools, start cmake-gui and set the source and build directories, then click Configure, in the list of options you will find Boost_INCLUDE_DIR and Boost_LIBRARY_DIR_RELEASE - change these to suit the boost libs you want to use and then click Configure and Generate. Then you can open the xcode project and compile.
If you are using the terminal to do the build you can add options when using cmake to create the build, setting BOOST_ROOT at this stage will allow cmake to find the boost libs during the initial configuration.
cmake -G Xcode -DBOOST_ROOT=~/customlibs ../blender

Visual studio and dlib: "cannot open include file: 'zlib.h': No such file or directory"

For my thesis I want to use Dlib's face_landmark_detection, but I keep running into these errors (for both Visual studio 2013 as well as 2015):
"cannot open include file: 'zlib.h': No such file or directory"
and
"'F77_INT': undeclared identifier".
It repeats itself so I have 36 errors based on these two problems.
My supervisor has given me some steps to follow to set up the project:
add dlib-master and dlib-master\examples to VC++ directories -> include directories
add dlib-master\dlib\external\libjpeg and dlib-master\dlib\entropy_decoder to C/C++ -> General -> Additional include directories
add all folders and items from dlib-master\dlib\external (cblas, libjpeg, libpng and zlib) to the project source folder
add the dlib source file (from dlib-master\dlib\all) and add face_landmark_detection (from dlib-master\examples) to the project source folder.
and according to him this has worked on every other computer so far, but on my laptop it just won't. We checked to project, but zlib.h is in the zlib folder in the project. Does anyone here have an idea on what might be going wrong?
If I didn't give enough info, please ask. I don't know what else might be needed to solve this.
I have just come about this same problem and wanted to post my solution since I have found so much conflicting documentation on the subject.
The folder containing the dlib folder as well as the libpng, libjpeg, and zlib folders from dlib/external need to be added to the additional include directories list in the solution settings.
dlib/all/source.cpp as well as the source files for libpng, libjpeg, and zlib also need to be added to the project.
Note that CBLAS should not be added to the project in any way, because it needs Fortran to compile, and it is very difficult to get this to compile from Visual Studio.
Finally, make sure to add DLIB_PNG_SUPPORT and DLIB_JPEG_SUPPORT as preprocessor defines in the project settings.
I also attempted to use a cmake generated solution, however, for some reason it had trouble with png support.
It is probably easiest to use CMake to configure your project which uses dlib. It avoids setting all those paths manually. During CMake configure step you can disable usage of libraries like zlib which you don't have/want/need. Here is an example CMakeLists.txt which works for me:
cmake_minimum_required(VERSION 2.6)
PROJECT(DatasetClassifier CXX C)
set(dlib_DIR "" CACHE PATH "Path to dlib") # http://dlib.net/
include(${dlib_DIR}/dlib/cmake)
ADD_EXECUTABLE(DatasetClassifier DatasetClassifier.cpp)
TARGET_LINK_LIBRARIES(DatasetClassifier ${dlib_LIBRARIES})

Cmake Custom Target not selected for configuration

I have cmake a custom command and custom target which runs doxygen to generate documentation from the header files.
However Visual Studio doesn't build this target when the build is started from command line. It says "Project not selected to build for this solution configuration". Is there a way make sure this target is selected for the release (or debug) configuration in Visual Studio?
You probably want the add_dependencies command.
After you have defined they doxygen target and an executable or library target, write something like
add_dependencies( my_executable my_custom_doxygen_target )
This will force the doxygen target to be built every time your executable is.
It may also be possible to make it work by turning off the EXCLUDE_FROM_ALL property on your doxygen target. I expected to see EXCLUDE_FROM_ALL mentioned in the description of add_custom_target, but didn't - so it might be different than I remember.

Resources