我通过选择Debug Multithreaded / SingleThreaded配置将Veretype 2.9从VS2017中的源代码构建到静态库中。看起来,静态库放在freetype-2.9 \ objs \ x64 \ Debug Static \ freetype.lib中。
在VS2017中,在附加库目录中,我添加了freetype-2.9 \ objs \ x64 \ Debug Static。在Additional Dependencies中,我添加了freetype.lib。并将运行时库设置为MTd。但是,编译会抛出链接器错误:
1>------ Build started: Project: HelloFreetype, Configuration: Debug x64 ------
1>Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol __imp_FT_Init_FreeType referenced in function main
1>Source.obj : error LNK2019: unresolved external symbol __imp_FT_Done_FreeType referenced in function main
1>C:\Users\joaqo\Documents\HelloFreetype\x64\Debug\HelloFreetype.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "HelloFreetype.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Freetype对预处理器有不寻常的用途,所以这里也是代码:
#include <ft2build.h>
#include FT_FREETYPE_H
int main(int argc, char **argv)
{
FT_Library library;
int error = FT_Init_FreeType(&library);
if (error) {
printf("FreeType: Initilization error\n");
exit(EXIT_FAILURE);
}
FT_Done_FreeType(library);
exit(EXIT_SUCCESS);
}
x86平台发布相同的错误,发布配置和/或将Windows SDK重新定位到8.1(Freetype也是使用SDK 8.1构建的)。使用Freetype 2.7.1也没有成功。并尝试链接到动态库是没有问题的!
谢谢你的帮助!
我按照步骤重现了相同的链接器错误,但是使用了VS2013。在构建FreeType时,我注意到了几个C4273编译器警告,如下所示:
1>..\..\..\src\base\ftinit.c(321): warning C4273: 'FT_Init_FreeType' : inconsistent dll linkage
1> C:\libraries\freetype-2.9\include\freetype/freetype.h(1987) : see previous definition of 'FT_Init_FreeType'
为了解决这些编译器警告,我编辑了config/ftconfig.h
FreeType头文件。我更改了以下行,
#define FT_EXPORT( x ) __declspec( dllimport ) x
至,
#define FT_EXPORT( x ) extern x
然后重建FreeType。在这些更改之后,链接器错误不再发生。
我相信FreeType 2.9中此问题的原因是由于ftconfig.h中FT_EXPORT定义的更改。
// From ftconfig.h - FreeType 2.9
#ifndef FT_EXPORT
#ifdef __cplusplus
#define FT_EXPORT( x ) extern "C" x
#else
#define FT_EXPORT( x ) extern x
#endif
#ifdef _MSC_VER
#undef FT_EXPORT
#ifdef _DLL
#define FT_EXPORT( x ) __declspec( dllexport ) x
#else
#define FT_EXPORT( x ) __declspec( dllimport ) x
#endif
#endif
#endif /* !FT_EXPORT */
注意_MSC_VER部分将如何撤消前面的定义。早期版本的FreeType中不存在此_MSC_VER块。
如果你只想构建一个静态库(没有DLL),那么删除_MSC_VER部分,如下所示:
#ifndef FT_EXPORT
#ifdef __cplusplus
#define FT_EXPORT( x ) extern "C" x
#else
#define FT_EXPORT( x ) extern x
#endif
#endif /* !FT_EXPORT */