我尝试用cmake和conan为MS Visual Studio构建freeGLUT项目,但是当我构建它时返回链接错误LNK1104 "无法打开文件" freeGLUT .lib"无法打开文件"freeglutd.lib"为调试。
Visual Studio 16 2019
Cmake 3.20.3
柯南1.39.0
项目树
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;
}
编辑
在CMakeLists.txt中我添加了
...
if(${FreeGLUT_FOUND})
message(STATUS "-----LIB FOUND!!!-----")
target_include_directories(${PROJECT_NAME} PUBLIC ${FreeGLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${FreeGLUT_LIBRARIES})
endif()
...
来自控制台的信息。
$ 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]
我认为这里的问题是在免费的情况下。柯南文件默认在包装中说:
self.cpp_info.components["freeglut_"].names["pkg_config"] = "freeglut" if self.settings.os == "Windows" else "glut"
它试着找到freeglut
同时默认conanfile设置"replace_glut": True
。
option to build either as "glut" (ON) or "freeglut" (OFF)
所以你的库被构建为glut
,但conanfile仍然试图找到freeglut
。我建议您尝试使用选项replace_glut=False
构建conan包
如果解决了这个问题,请在这里打开一个bug: https://github.com/conan-io/conan-center-index/issues