简体   繁体   中英

How to statically link FreeType2 in Visual Studio?

I built Freetype 2.9 from source in VS2017 into a static library by choosing Debug Multithreaded/SingleThreaded configuration. Seemingly, the static library is placed in freetype-2.9\\objs\\x64\\Debug Static\\freetype.lib .

In VS2017, in Additional Library Directories I added freetype-2.9\\objs\\x64\\Debug Static . In Additional Dependencies I added freetype.lib . And set Runtime Library to MTd . However compilation throws the linker errors:

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 has unusual uses for the preprocessor, so here is the code as well:

#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);
}

Same error happens with x86 platform, release configuration and/or retargeting Windows SDK to 8.1 (Freetype was built with SDK 8.1 too). Also tried without success with Freetype 2.7.1. And trying to link to dynamic library is no problem at all!

Thanks for any help!

I reproduced the same linker errors by following the steps, but using VS2013. While building FreeType, I noticed several C4273 compiler warnings such as the following:

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'

To resolve these compiler warnings, I edited the config/ftconfig.h FreeType header file. I changed the following line,

#define FT_EXPORT( x )  __declspec( dllimport )  x

to,

#define FT_EXPORT( x ) extern x

then rebuilt FreeType. Following these changes the linker errors no longer occur.

I believe the cause of this problem in FreeType 2.9 is due a change to the definition of FT_EXPORT in ftconfig.h.

// 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 */

Notice how the _MSC_VER portion will undo the preceding defines. This _MSC_VER block was not present in earlier versions of FreeType.

If you just want to build a static lib (no DLL), then remove the _MSC_VER portion like so:

#ifndef FT_EXPORT
    #ifdef __cplusplus
    #define FT_EXPORT( x )  extern "C"  x
    #else
    #define FT_EXPORT( x )  extern  x
    #endif
#endif /* !FT_EXPORT */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM