Build systems

How to fix CMake build error on Ubuntu: Could NOT find HarfBuzz (missing: HarfBuzz_INCLUDE_DIR HarfBuzz_LIBRARY

Problem:

While building a software project using cmake, you see error messages like

CMake Error at /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find HarfBuzz (missing: HarfBuzz_INCLUDE_DIR HarfBuzz_LIBRARY
  _HarfBuzz_REQUIRED_LIBS_FOUND)
Call Stack (most recent call first):
  /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindHarfBuzz.cmake:153 (find_package_handle_standard_args)
  CMakeLists.txt:801 (find_package)

Solution:

Install libharfbuzz-dev

sudo apt -y install libharfbuzz-dev

 

Posted by Uli Köhler in CMake, Linux

How to fix CMake build error on Ubuntu: Could NOT find GLM (missing: GLM_INCLUDE_DIR GLM_VERSION)

Problem:

While building a software project using cmake, you see error messages like

CMake Error at /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find GLM (missing: GLM_INCLUDE_DIR GLM_VERSION) (Required is at
  least version "0.9.8")
Call Stack (most recent call first):
  /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindGLM.cmake:54 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:751 (find_package)

Solution:

Install libglm-dev

sudo apt -y install libglm-dev

 

Posted by Uli Köhler in CMake, Linux

How to fix CMake build error on Ubuntu: Could NOT find GLEW (missing: GLEW_INCLUDE_DIR GLEW_LIBRARY)

Problem:

While building a software project using cmake, you see error messages like

CMake Error at /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find GLEW (missing: GLEW_INCLUDE_DIR GLEW_LIBRARY)
Call Stack (most recent call first):
  /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindGLEW.cmake:38 (find_package_handle_standard_args)
  CMakeLists.txt:743 (find_package

Solution:

Install libglew-dev

sudo apt -y install libglew-dev

 

Posted by Uli Köhler in CMake, Linux

How to fix CMake “Could not find a package configuration file provided by “Qt5Core” on Ubuntu?

Problem:

When trying to configure your project using e.g.

cmake .

you see the following error messages:

CMake Warning at CMakeLists.txt:120 (find_package):
  By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt5Core", but
  CMake did not find one.

  Could not find a package configuration file provided by "Qt5Core" with any
  of the following names:

    Qt5CoreConfig.cmake
    qt5core-config.cmake

  Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set
  "Qt5Core_DIR" to a directory containing one of the above files.  If
  "Qt5Core" provides a separate development package or SDK, be sure it has
  been installed.

Solution:

Install qtbase5-dev:

sudo apt -y install qtbase5-dev

 

 

Posted by Uli Köhler in C/C++, CMake

How to get dependency include & library directories in Conanfile.py

Within conanfile.py , this is how you can obtain a list of include & library directories for each of the dependencies:

class MyProject(Conanfile):
    # ...
    def build(self):
        # Check dependencies
        for dependency in self.dependencies.values():
            cppinfo = dependency.cpp_info.aggregated_components()
            print("libdirs", cppinfo.libdirs)
            print("includedirs", cppinfo.includedirs)
        # ...

 

Posted by Uli Köhler in Conan

How to use ‘conan download’ to download a package from conancenter

When you try to download a package from conancenter using conan download, the right syntax to use is:

conan download libcurl/8.1.1 -r conancenter

If you forget to specify -r conancenter, you will see the following error message:

usage: conan download [-h] [-v [V]] [--only-recipe] [-p PACKAGE_QUERY] -r REMOTE reference
conan download: error: the following arguments are required: -r/--remote
ERROR: Exiting with code: 2

 

Posted by Uli Köhler in Conan

How to fix conan ERROR: The default build profile doesn’t exist.

Problem:

You want to install or build a conan project using a command such as

conan install .

but you see an error message such as

ERROR: The default build profile '/home/uli/.conan2/profiles/default' doesn't exist.
You need to create a default profile (type 'conan profile detect' command)
or specify your own profile with '--profile:build=<myprofile>'

Solution:

Typically you just want to create the default build profile using

conan profile detect

detect means to detect the compiler.

Posted by Uli Köhler in Conan

List of conan 2.x pre-defined templates

The following list of templates is predefined for conan 2.0.6:

  • basic
  • cmake_lib
  • cmake_exe
  • meson_lib
  • meson_exe
  • msbuild_lib
  • msbuild_exe
  • bazel_lib
  • bazel_exe
  • autotools_lib
  • autotools_exe

You can check for yourself using

conan new --help

In the output from this command, look for positional arguments -> template. In the help text, you’ll find a list of possible templates.

Posted by Uli Köhler in Conan

How to fix conan new ERROR: Missing definitions for the template. Required definitions are: ‘name’, ‘version’

Problem:

You want to initialize your conan project using a command such as

conan new cmake_exe

but you see the following error message:

ERROR: Missing definitions for the template. Required definitions are: 'name', 'version'

Solution:

Conan wants you to specify the name and version of your package using -d name=myproject -d version=1.0.0.

Full example:

conan new cmake_exe -d name=MyPkg -d version=1.0.0

 

Posted by Uli Köhler in Conan

How to fix conan ERROR: Template doesn’t exist or not a folder

Problem:

You want to initialize a conan project using a command such as

conan new myproject/1.0.0

However, once you run it, you see an error message like

ERROR: Template doesn't exist or not a folder: myproject/1.0.0

Solution:

The way you have to use conan new changed with Conan version 2.0.

Now you don’t specify the project name but a template! Templates are names such as cmake_exe but the easiest template to start with is basic as it doesn’t require any parameters:

conan new basic

You can also initialize it using a predefined template such as cmake_exe:

conan new cmake_exe -d name=MyProject -d version=1.0.0

 

Posted by Uli Köhler in Conan

How to initialize C++ project using conan 2.x

The simplest way of starting is by running the following command in your project directory:

conan new basic

This will create conanfile.py from the basic template.

Alternatively, you can initialize it from a predefined template such as cmake_exe:

conan new cmake_exe -d name=myproject -d version=1.0.0

 

Posted by Uli Köhler in C/C++, Conan

How to configure CMake to use C++20

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

 

Posted by Uli Köhler in Build systems, C/C++, CMake

How to fix SCons error: Do not know how to make File target `clean’

Problem:

You are trying to clean your SCons build using

scons clean

but you see the following error message:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: *** Do not know how to make File target `clean' (/home/user/myproject/clean).  Stop.
scons: building terminated because of errors.

Solution:

When using SCons, the correct command to clean is

scons --clean

 

Posted by Uli Köhler in Build systems, SCons

How to fix SCons not providing colored GCC/C++ output

You can force SCons to provide colored output when using GCC or G++ by adding

-fdiagnostics-color=always

to your CFLAGS or CXXFLAGS. Typically, my recommendation is to add it to both CFLAGS and CXXFLAGS.

Posted by Uli Köhler in Build systems, SCons

Minimal CMake .gitignore

# CMake generated files
build/
CMakeCache.txt
CMakeFiles/
Makefile
cmake_install.cmake
install_manifest.txt

 

Posted by Uli Köhler in CMake, git

How to include current directory using CMake

It is often helpful to include the current directory for C++ projects. When using GCC directly, this can be done using -I., whereas for CMake you need to use the following statement:

target_include_directories(myexe PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

Remember to replace myexe by the name of your executable target.

Posted by Uli Köhler in CMake

How to fix CMake Error: Cannot find source file: *.cpp

Problem:

You are trying to build your CMake-based project using a CMakeLists.txt like

cmake_minimum_required(VERSION 3.10)
project(my_project)

add_executable(my_project *.cpp)

Solution:

You can’t use the glob pattern *.cpp directly in add_executable(). You need to use file(GLOB ...) in order to set a variable, which you can then use in add_executable():

cmake_minimum_required(VERSION 3.10)
project(my_project)

file(GLOB SRC_FILES *.cpp)
add_executable(my_project ${SRC_FILES})

 

Posted by Uli Köhler in CMake

How to fix CMake “target_include_directories called with invalid arguments”

Problem:

In your CMakeLists.txt, you want to add an include directory such as /usr/include/mylibrary for the executable myexe using code like:

target_include_directories( myexe /usr/include/mylib )

But when you try to configure the build using cmake . or make, you see an error message like

CMake Error at CMakeLists.txt:8 (target_include_directories):
  target_include_directories called with invalid arguments


-- Configuring incomplete, errors occurred!

Solution:

You need to add PUBLIC between the target name (myexe) and the include directory/directories:

target_include_directories( myexe PUBLIC /usr/include/mylib )

 

Posted by Uli Köhler in CMake

Conan .gitignore for executable projects

This is the .gitignore I use for most of my conan-built executable projects:

CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.json

It is debatable whether you should include lock files such as conan.lock. My opinion is that in the development phase they should be ignored but reconsidered when entering into production service.

Posted by Uli Köhler in Conan, git

How to initialize new C++ executable project using conan 1.x

Important note: This post is for Conan 1.x and is not compatible with the current version conan 2.x! See How to initialize C++ project using conan 2.x for an updated version of this post!

In the directory where you want to create the project, run (obviously, replace mypackageby the name of everywhere in our code!)

conan new mypackage/1.0.0 -s

-s is important here. It tells conan that the source is available in the project directory and it should not be pulled using git etc.

This will initialize a shared library project, which we will now modify to build an executable.

First, open src/CMakeLists.txt and change add_library() to add_executable(), for example

add_library(mypackage mypackage.cpp)

to

add_executable(mypackage mypackage.cpp)

Now we shall modify conanfile.py to properly build & install the executable:

    def package(self):
        self.copy("*.h", dst="include", src="src")
        self.copy("*.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.dylib*", dst="lib", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)
    def package_info(self):
        self.cpp_info.libs = ["NoxecoDB"]

with

    def package(self):
        self.copy("mypackage", src="bin", dst="bin", keep_path=False)

    def package_info(self):
        self.env_info.PATH = os.path.join(self.package_folder, "bin")

    def deploy(self):
        self.copy("mypackage", dst="bin", src="bin")

and add

import os.path

to the top of the file

Now let’s add a main() function to  src/mypackage.cpp. Replace the file’s content with

#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Hello World!" <<std::endl;
}

Now it’s time to install the dependencies & build the project using

conan install . && conan build .

The executable will be in

bin/mypackage

You can run it using

./bin/mypackage

which will print

Hello World!

I also recommend to add this .gitignore:

CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.json

 

Posted by Uli Köhler in C/C++, Conan