Conan: [question] How to use glfw and xorg recipes with cmake_find_package generator

Created on 31 Aug 2020  路  3Comments  路  Source: conan-io/conan

I'm trying to build an app based on GLFW on Ubuntu 18.04 and use conan and CMake to manage dependencies. The app fails to link because of erros like undefined reference to XFree. I looked at the files generated by CMake and found that it's not linking to X11. I was using the cmake_find_package generator.

A minimal example showing the problem is like this:

conanfile.txt

[requires]
glfw/3.3.2

[generators]
cmake_find_package

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(cppplayground LANGUAGES CXX)

set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})

find_package(glfw REQUIRED)

add_executable(cppplayground main.cpp)
target_link_libraries(cppplayground glfw::glfw)

main.cpp

#include <iostream>
#include <GLFW/glfw3.h>

int main() {
  if (glfwInit()) {
    std::cout << "glfw inited" << std::endl;
  } else {
    std::cout << "glfw not inited" << std::endl;
  }
  return 0;
}

And the outputs:

conan

Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=8
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

conanfile.txt: Installing package
Requirements
    glfw/3.3.2 from 'conan-center' - Cache
    opengl/system from 'conan-center' - Cache
    xorg/system from 'conan-center' - Cache
Packages
    glfw/3.3.2:4c59a41a5d4c30361feed124bbf9b7f83a55d486 - Cache
    opengl/system:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache
    xorg/system:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache

Installing (downloading, building) binaries...
opengl/system: Already installed!
xorg/system: Already installed!
glfw/3.3.2: Already installed!
conanfile.txt: Generator cmake_find_package created Findglfw.cmake
conanfile.txt: Generator cmake_find_package created Findopengl.cmake
conanfile.txt: Generator cmake_find_package created Findxorg.cmake
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo

CMake

-- The CXX compiler identification is GNU 8.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Using autogenerated Findglfw.cmake
-- Found glfw: 3.3.2 (found version "3.3.2") 
-- Library glfw3 found /home/chubei/.conan/data/glfw/3.3.2/_/_/package/4c59a41a5d4c30361feed124bbf9b7f83a55d486/lib/libglfw3.a
-- Found: /home/chubei/.conan/data/glfw/3.3.2/_/_/package/4c59a41a5d4c30361feed124bbf9b7f83a55d486/lib/libglfw3.a
-- Conan: Using autogenerated Findopengl.cmake
-- Found opengl: system (found version "system") 
-- Conan: Using autogenerated Findxorg.cmake
-- Found xorg: system (found version "system") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chubei/cppplayground/build

make

/home/chubei/.conan/data/glfw/3.3.2/_/_/package/4c59a41a5d4c30361feed124bbf9b7f83a55d486/lib/libglfw3.a(x11_monitor.c.o): In function `_glfwPollMonitorsX11':
x11_monitor.c:(.text+0x3f4): undefined reference to `XFree'
...and many other similar errors

The environment:

Conan version 1.28.1
Python 3.7.7
Ubuntu 18.04.5 LTS
cmake version 3.17.0
gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
bug

Most helpful comment

Hi czoido, with Conan 1.28.2, the app builds and runs fine. Thank you for your quick response and brilliant work.

All 3 comments

Hi @chubei-oppen,
Thanks a lot for reporting. I think this is related to a bug we detected, where the system libs were not added to the targets when using componentes. The fix be released very soon (today or tomorrow) in Conan 1.28.2
This is the PR for that bug: https://github.com/conan-io/conan/pull/7611
I'm going to close the issue as this should be addressed by that, but please feel free to reopen if you get errors when you update to 1.28.2

@chubei-oppen, we have just released Conan 1.28.2, please try with the new version and report if your issues are not solved.

Hi czoido, with Conan 1.28.2, the app builds and runs fine. Thank you for your quick response and brilliant work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theodelrieu picture theodelrieu  路  3Comments

uilianries picture uilianries  路  3Comments

bobeff picture bobeff  路  3Comments

liberforce picture liberforce  路  3Comments

uilianries picture uilianries  路  3Comments