如何编写 CMakeLists.txt 以使用 FreeImage 预编译库和头文件
How to write a CMakeLists.txt to use FreeImage precompiled library and header file
文件夹结构:
Project/
├── main.cpp
└── deps/
└── FreeImage/
├── FreeImage.lib
├── FreeImage.dll
├── FreeImage.h
├── FreeImagePlus.lib
├── FreeImagePlus.dll
└── FreeImagePlus.h
代码:
#include <FreeImagePlus.h>
int main(void)
{
fipImage i;
return 0;
}
现在的问题:
如何编写CMakeLists.txt文件以便能够在Windows中编译上述内容?
我的尝试作为下面的答案
您应该在此处使用不同的步骤。一个正确的目标是在FindFreeImage.cmake
文件中拥有使用 FreeImage 所需的一切。然后你可以像这样设置FreeImage.cmake
:
FIND_PATH(FreeImage_INCLUDE_DIR FreeImage.h HINTS ${FreeImage_ROOT})
FIND_LIBRARY(FreeImage_LIBRARY NAMES FreeImage HINTS ${FreeImage_ROOT})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FreeImage DEFAULT_MSG FreeImage_INCLUDE_DIR FreeImage_LIBRARY)
当然,您应该添加更多内容,以便在链接到FreeImage
时,您可以获得包含路径集,安装过程...但那是另一个问题。
CMakeList的一个解决方案.txt带有额外的提示
cmake_minimum_required(VERSION 3.8)
set(NAME cmakecopytest)
project(${NAME})
# The place to put built libraries
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin/windows-64/debug")
# Copying pre-compiled dll libraries next to the built libraries for them to be found in runtime without extra effort
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.dll" "${CMAKE_BINARY_DIR}/bin/windows-64/debug/FreeImagePlus.dll" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.dll" "${CMAKE_BINARY_DIR}/bin/windows-64/debug/FreeImage.dll" COPYONLY)
add_executable(${NAME}_exe main.cpp)
target_include_directories(${NAME}_exe PUBLIC ./ include/ deps/FreeImage)
target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.lib" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.lib")
# target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus") # ERROR LNK1104: cannot open file '..depsFreeImageFreeImage.obj'
额外提示:
- CMAKE_BINARY_DIR包含生成解决方案的位置(即 D:/工作/项目/生成(
设置输出目录,然后将预编译库复制到同一位置是一种简单的方法,无需在 MSVS 中添加任何额外的配置即可运行代码
math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8") set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits}) foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL) foreach(var CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config} CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config} CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config} ) set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}") string(TOLOWER "${${var}}" ${var}) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus.dll ${var}/FreeImagePlus.dll COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage.dll ${var}/FreeImage.dll COPYONLY) endforeach() endforeach()
对于所有所需的配置,这 for cycle 为您做到这一点
但是,我不喜欢在target_link_libraries(...FreeImage.lib...)
中指定".lib"
其他人有一个解决方案,可以在避免链接错误的同时说target_link_libraries(${NAME}_exe PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImage" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/deps/FreeImage/FreeImagePlus")
LNK1104: cannot open file '..depsFreeImageFreeImage.obj'
?
相关文章:
- 使用 MEX 编译C++文件
- 获取 clang++:错误:在编译文件系统库的代码时
- 如何交叉编译C++文件
- 使用 MinGW 在 Windows 上的命令行编译C++文件
- 选择特定版本的 Visual Studio 命令行工具包,并根据特定版本的C++运行时环境编译文件
- 解析模板和范围后获取编译文件?
- 我的 PowerShell 脚本无法按预期工作(用于编译C++文件)
- 如何在不制作Visual Studio 2017项目的情况下编译C++文件?
- 'void *' 'void (*) (bool)' 指针使用 c ++ 编译器在 C 中编译文件的错误
- 在没有节点GYP的目标平台上编译C/C 文件
- 生成文件似乎以错误的顺序编译文件
- 编译 C++ 文件失败,因为找不到"ssize_t"的定义
- NeatBeans Makefile:麻烦编译.H文件
- 我无法编译文件系统库
- 在不使用预编译头C++项目中编译 C 文件
- 无法使用 Xcode 和终端在 Mac OS 上使用 OpenGL 库编译 c++ 文件
- 获取编译文件的列表,并由Make Project包含
- MinGW .h 编译文件不起作用:此文件的版本与您运行的 Windows 版本不兼容
- 编译 c++ 文件时出错:未定义的符号
- C++编译文件