0

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!

Joaqo
  • 51
  • 1
  • 7
  • you define `FT_Init_FreeType` and `FT_Done_FreeType` with `__declspec(dllimport)` which is already error if you want static library. now search for `FT_Init_FreeType` in `freetype.lib` - in which form it present here ? may be in form `__imp_?FT_Init_FreeType@@...` ? are it declared as *c* or *c++* function ? – RbMm Jan 28 '18 at 19:23
  • well yes, i saw somewhere that the '__imp__' had something to do with dll, and if I open the *freetype.lib* with 7zip, I can see some symbols in the form 'FT_Init_FreeType', without __imp__. So yes, I guess I should be linking to symbols without __imp__. However, how can I tell VS to look for the correct symbols? – Joaqo Jan 28 '18 at 20:36
  • which is name **exactly** in *freetype.lib* ? 'FT_Init_FreeType' ? without any `?` and `@` symbols ? the `__imp_` prefix produced by `__declspec(dllimport)` in function declaration. remove it – RbMm Jan 28 '18 at 20:39
  • In *freetype.lib*, there is a text file with a bunch of lines like this: ´..\..\..\objs\Win32\Debug Static\ftinit.obj _FT_Init_FreeType´ – Joaqo Jan 29 '18 at 00:29
  • the symbol name in *freetype.lib* must be **exactly** to symbol name used in *HelloFreetype*. `__imp_FT_Init_FreeType` say about you use `__declspec(dllimport)` in declaration of `FT_Init_FreeType` - remove it ! after this error must disappear or will be unresolved `FT_Init_FreeType` symbol. `_FT_Init_FreeType` - are you sure about `_` in the front ? this say about you look **x86** lib file. you can also run `link.exe /dump /LINKERMEMBER freetype.lib > freetype.txt` and look it public symbols. how I say name must be **exactly** match. symbol to symbol. – RbMm Jan 29 '18 at 05:20

2 Answers2

1

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.

bitvortex
  • 11
  • 1
0

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 */
T. Russell
  • 33
  • 5