How to get rid of link error in FreeGLUT MS Visual Studio project build with Cmake + Conan?

Im trying build freeGLUT project with cmake and conan for MS Visual Studio, but when I build it return link error LNK1104 “Cant open file “freeglut.lib” for Release and “Cant open file “freeglutd.lib” for Debug.

Visual Studio 16 2019

Cmake 3.20.3

Conan 1.39.0

Project Tree

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(OpenGLConan)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)

add_subdirectory(src)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(FreeGLUT)

target_include_directories(${PROJECT_NAME} PUBLIC ${FreeGLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${FreeGLUT_LIBRARIES})

conanfile.txt

[requires]
freeglut/3.2.1

[generators]
cmake_find_package
cmake_paths

src/CMakeLists.txt

add_executable(${PROJECT_NAME} main.cpp)

src/main.cpp

#include <iostream>
#include <GL/glut.h>

int main(int argc, char** argv)
{
    glutInit(&argc, argv);

    /* Create a single window with a keyboard and display callback */
    glutCreateWindow("GLUT Test");

    /* Run the GLUT event loop */
    glutMainLoop();

    return EXIT_SUCCESS;
}

Edit

In CMakeLists.txt I added

...
if(${FreeGLUT_FOUND})
    message(STATUS "-----LIB FOUND!!!-----")
    target_include_directories(${PROJECT_NAME} PUBLIC ${FreeGLUT_INCLUDE_DIRS})
    target_link_libraries(${PROJECT_NAME} ${FreeGLUT_LIBRARIES})
endif()
...

Information from the console.

$ conan install ..
Configuration:
[settings]
arch=x86_64
arch_build=x86_64     
build_type=Release    
compiler=Visual Studio
compiler.runtime=MD   
compiler.version=16   
os=Windows
os_build=Windows      
[options]
[build_requires]      
[env]

conanfile.txt: Installing package
Requirements
    freeglut/3.2.1 from 'conancenter' - Cache
    glu/system from 'conancenter' - Cache
    opengl/system from 'conancenter' - Cache
Packages
    freeglut/3.2.1:d72ca9d65490ffa06c751d72974c9906d3454d5c - Cache
    glu/system:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache    
    opengl/system:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache 

Installing (downloading, building) binaries...
opengl/system: Already installed!
glu/system: Already installed!
freeglut/3.2.1: Already installed!
conanfile.txt: Generator cmake_paths created conan_paths.cmake
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generator cmake_find_package created FindFreeGLUT.cmake
conanfile.txt: Generator cmake_find_package created Findglu.cmake
conanfile.txt: Generator cmake_find_package created Findopengl_system.cmake
conanfile.txt: Aggregating env generators
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo

Антон@LAPTOP-4L8RSG2A MINGW64 /d/GitHub/OpenGLConan/build
$ cmake .. -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.29.30038.1
-- The CXX compiler identification is MSVC 19.29.30038.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Using autogenerated FindFreeGLUT.cmake
-- Found FreeGLUT: 3.2.1 (found version "3.2.1") 
-- Library glut found C:/Users/Anton/.conan/data/freeglut/3.2.1/_/_/package/d72ca9d65490ffa06c751d72974c9906d3454d5c/lib/glut.lib
-- Found: C:/Users/Anton/.conan/data/freeglut/3.2.1/_/_/package/d72ca9d65490ffa06c751d72974c9906d3454d5c/lib/glut.lib
-- Conan: Using autogenerated Findopengl_system.cmake
-- Found opengl_system: system (found version "system") 
-- Conan: Using autogenerated Findglu.cmake
-- Found glu: system (found version "system")
-- Dependency opengl_system already found
-- Library glut found C:/Users/Anton/.conan/data/freeglut/3.2.1/_/_/package/d72ca9d65490ffa06c751d72974c9906d3454d5c/lib/glut.lib
-- Found: C:/Users/Anton/.conan/data/freeglut/3.2.1/_/_/package/d72ca9d65490ffa06c751d72974c9906d3454d5c/lib/glut.lib
-- -----LIB FOUND!!!-----
-- Configuring done
-- Generating done
-- Build files have been written to: D:/GitHub/OpenGLConan/build

Антон@LAPTOP-4L8RSG2A MINGW64 /d/GitHub/OpenGLConan/build
$ cmake --build .
Microsoft (R) Build Engine версии 16.10.2+857e5a733 для .NET Framework
(C) Корпорация Майкрософт (Microsoft Corporation). Все права защищены.

  Checking Build System
  Building Custom Rule D:/GitHub/OpenGLConan/src/CMakeLists.txt
  main.cpp
LINK : fatal error LNK1104: не удается открыть файл "freeglutd.lib" [D:GitHubOpenGLConanbuildsrcOpenGLConan.vcxproj]

Answer

I think the problem here is in the conanfile of freeglut. Conan file be default says in packaging:

self.cpp_info.components["freeglut_"].names["pkg_config"] = "freeglut" if self.settings.os == "Windows" else "glut"

So it tries to find freeglut.

At the same time be default conanfile sets "replace_glut": True. From Cmake this option sets:

option to build either as "glut" (ON) or "freeglut" (OFF)

So your library is built as glut, but conanfile still tries to find freeglut. I would suggest you try to build conan package with option replace_glut=False

If that fixes the issue, pls open a bug here: https://github.com/conan-io/conan-center-index/issues