如何摆脱使用 Cmake + Conan 构建的 FreeGLUT MS Visual Studio 项目中的 link 错误?
How to get rid of link error in FreeGLUT MS Visual Studio project build with Cmake + Conan?
我正在尝试使用 cmake 和 conan 为 MS Visual Studio 构建 freeGLUT 项目,但是当我构建它时 return link 错误 LNK1104“无法打开文件“freeglut.lib”对于发布和“无法打开文件”freeglutd.lib”对于调试。
Visual Studio 16 2019
CMake 3.20.3
柯南 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;
}
编辑
在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:\GitHub\OpenGLConan\build\src\OpenGLConan.vcxproj]
我认为这里的问题出在 freeglut 的 conanfile 中。柯南文件默认在包装中说:
self.cpp_info.components["freeglut_"].names["pkg_config"] = "freeglut" if self.settings.os == "Windows" else "glut"
所以它试图找到 freeglut
。
同时被默认的conanfile集"replace_glut": True
。从 Cmake 这个选项集:
option to build either as "glut" (ON) or "freeglut" (OFF)
因此您的库构建为 glut
,但 conanfile 仍会尝试查找 freeglut
。
我建议您尝试使用 replace_glut=False
选项构建柯南包
如果这解决了问题,请在此处打开一个错误:https://github.com/conan-io/conan-center-index/issues
我正在尝试使用 cmake 和 conan 为 MS Visual Studio 构建 freeGLUT 项目,但是当我构建它时 return link 错误 LNK1104“无法打开文件“freeglut.lib”对于发布和“无法打开文件”freeglutd.lib”对于调试。
Visual Studio 16 2019
CMake 3.20.3
柯南 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;
}
编辑
在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:\GitHub\OpenGLConan\build\src\OpenGLConan.vcxproj]
我认为这里的问题出在 freeglut 的 conanfile 中。柯南文件默认在包装中说:
self.cpp_info.components["freeglut_"].names["pkg_config"] = "freeglut" if self.settings.os == "Windows" else "glut"
所以它试图找到 freeglut
。
同时被默认的conanfile集"replace_glut": True
。从 Cmake 这个选项集:
option to build either as "glut" (ON) or "freeglut" (OFF)
因此您的库构建为 glut
,但 conanfile 仍会尝试查找 freeglut
。
我建议您尝试使用 replace_glut=False
如果这解决了问题,请在此处打开一个错误:https://github.com/conan-io/conan-center-index/issues