'C++/C Importing Third Party Library to CMake

Hi I was wondering if anyone here could help me identify what I'm doing wrong while trying to add a library to my CMake project: So originally I built the library https://github.com/recp/cglm in the command line with cmake.Heres what I did

I created a build folder in the desktop(mkdir build)

  1. I changed directory to it (cd build)
  1. And then I created the sln with cmake(cmake /path-to/cglm)

After that I opened Visual Studio 2019 and saw 5 projects: ALL_BUILD, cglm, INSTALL, PACKAGE, ZERO_CHECK

I built the cglm project and recieved this in the Build Folder

Then inside the debug folder of the build folder I saw 4 files: cglm.exp , cglm.lib , cgl-0.dll and cglm-0.pdb

Then I went to another project to add the library and created the following CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("MathPlease")

add_executable(MathPlease "MathPlease.cpp" "MathPlease.h")

link_directories("path-to/desktop/dev/cglm/build

find_package("cglm")

When I try to save that I receive the following error

Severity    Code    Description Project File    Line    Suppression State
Warning     CMake Warning at C:\Users\asupr\source\repos\MathPlease\CMakeLists.txt:14 (find_package):
  By not providing "Findcglm.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "cglm", but
  CMake did not find one.

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

    cglmConfig.cmake
    cglm-config.cmake

  Add the installation prefix of "cglm" to CMAKE_PREFIX_PATH or set
  "cglm_DIR" to a directory containing one of the above files.  If "cglm"
  provides a separate development package or SDK, be sure it has been
  installed.    MathPlease  C:\Users\asupr\source\repos\MathPlease\CMakeLists.txt   14  

If anyone needs the cmakeoutput.log I can paste it here as well any help would be greatly appreciated!



Solution 1:[1]

In general you have two approaches -

  1. Install cglm library & then make use of it in your project
  2. Build cglm as part of your project

I have used both approaches & found the latter to be far better. Especially for smaller projects for these reasons -

  • Better intellisense, you can jump to 3rd party code and even edit
  • Easy to package and ship the project artifacts
  • Easy to manage in CI, version upgrades fo 3rd party projects

I use FectchContent CMake api to achieve this. (Alternatively same can be achieved by adding third party source-code to your project manually too)

Now I have not worked on cglm personally, but still a sample build file

cmake_minimum_required(VERSION 3.16)

project("MathPlease")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)

fetchcontent_declare(
  cglm
  GIT_REPOSITORY https://github.com/recp/cglm.git
  GIT_TAG v0.8.5
  GIT_PROGRESS TRUE
)

if(NOT cglm_POPULATED)
  message("populating cglm")
  fetchcontent_populate(cglm)
  add_subdirectory(${cglm_SOURCE_DIR} ${cglm_BUILD_DIR})
endif()

add_executable(${PROJECT_NAME} MathPlease.cpp)

target_link_libraries(${PROJECT_NAME} cglm)

P.S. FetchContent is a fairly recent CMake feature. You will need CMake > 3.11

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1