将自由字体与cmake链接 [英] Linking freetype with cmake
问题描述
在构建C ++ 11项目时,我在使用 cmake
在Linux下使用 cmake
链接 freetype 2
时遇到麻烦使用 extern C
库。
I'm having troubles with linking freetype 2
under linux using cmake
when building a C++11 project with an extern C
library.
使用cmake和freetype 2,我基本上有2个选择:
With cmake and freetype 2 I basically have 2 options :
- 使用实用程序
freetype-config
像freetype-config --libs
- 使用
FindFreetype
cmake模块
- use the utility
freetype-config
likefreetype-config --libs
- use the
FindFreetype
cmake module
现在我正在尝试实现第二个选项而且我对 cmake
不太熟练,也不了解它的逻辑。
Now I'm trying to implement the second option and I'm not very skilled with cmake
nor I understand the logic of it.
我的问题是链接阶段,我不知道如何正确执行此操作,而且此模块不如 freetype-config --libs
的结果那样完整,该模块实际上包括所有库和标志,我需要的,不仅仅是文件的路径;因此我假设我必须对 zlib
和 libpng
做同样的事情。
My problem is the linking phase, I have no idea how to do that properly plus this module is not as complete as the result of freetype-config --libs
which really includes all the libraries and flags that I need, and not just the path of a file; so I'm assuming that I have to do the same for zlib
and libpng
.
CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (FreetypeTutorials1)
include(FindFreetype)
include_directories(${FREETYPE_INCLUDE_DIRS})
SET(CMAKE_CXX_FLAGS "-O2 -std=c++11")
SET(CMAKE_EXE_LINKER_FLAGS "-v -lfreetype")
add_executable( demo "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
./ src / main.cpp
(只是
extern "C" {
#include <ft2build.h>
#include FT_FREETYPE_H
}
#include <iostream>
int main()
{
FT_Library library;
auto error = FT_Init_FreeType(&library);
if (error)
{
std::cout << "An error !\n";
}
}
推荐答案
到加载 FindFreetype.cmake
这样的模块,您需要使用 find_package
-命令在cmake中使用它。第一个参数是程序包名称。 cmake会自动添加其对应文件名的查找。
To load a module like FindFreetype.cmake
you need to use it in cmake with the find_package
-command. The first argument is the package name. The "Find" of its corresponding filename is added automatically by cmake.
include
可能与<$ c一起使用$ c> find_package ,您可以添加一些标志。例如,如下所示,必需
,当找不到自由类型时,使cmake失败。
While include
might work with find_package
you can add some flags. For example, as shown below, REQUIRED
, to make cmake fail when freetype wasn't found.
附加链接使用cmake应该通过命令 target_link_libraries
完成。
Additionally linking with cmake should be done with the command target_link_libraries
.
这就是我写给CMakeLists.txt的方式:
This is how I would write you CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (FreetypeTutorials1)
find_package(Freetype REQUIRED)
SET(CMAKE_CXX_FLAGS "-O2 -std=c++11")
SET(CMAKE_EXE_LINKER_FLAGS "-v")
add_executable( demo src/main.cpp) # CMAKE_CURRENT_SOURCE_DIR is implicit here
target_link_libraries(demo ${FREETYPE_LIBRARIES})
target_include_directories(demo PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries
与平台无关,而'-lfreetype
在 CMAKE_EXE_LINKER_FLAGS
不是。
target_link_libraries
is platform independent whereas '-lfreetype
in CMAKE_EXE_LINKER_FLAGS
is not.
CMakeLists.txt将在具有freetype的其他平台上运行。
The CMakeLists.txt will work on other platforms where freetype is available.
(编辑2019:使用 target_include_directories()
代替 include_directories()
(Edit 2019: use target_include_directories()
instead of include_directories()
这篇关于将自由字体与cmake链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!