Tarek Khalifa
Tarek Khalifa

Reputation: 27

Issue with static library linking for C++ (Unreal Engine 5 project)

So I'm trying to use the library utf8proc in my project. I've built the library using cmake, which has created a static lib file (utf8proc_static.lib), which I'm referencing in my Build.cs file using PublicAdditionalLibraries.Add. It seems to recognize the lib, since, if I make changes to the path, it immediately complains that it can't find the file, so all seems well on that front. I also included the location of the header file utf8proc.h, using PublicIncludePaths.Add.

In the cpp where I want to use the lib, I'm using #include <utf8proc.h>, and again all seems well, as I can travel to each function's definition over in the utf8proc.c file, using control+lmb. However, when I build the project it tells me there are 5 unresolved externals, and I'm getting 5 LNK2019 errors like this one:

Error LNK2019 unresolved external symbol __imp_utf8proc_iterate referenced in function "class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > __cdecl convertToUnicode(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?convertToUnicode@@YA?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z)

I've been troubleshooting for a while but I can't seem to get to the bottom of it. I'm using other dlls and libs, and I'm not having issues with any other library. I think this is the only static lib I'm using though.

I've tried deleting UE's binaries and intermediate folders and regenerating the visual studio project files. Tried adding the folders containing the.lib and .h files to the VC++ directories via VS22 project settings. Out of desperation I've also tried moving the lib file over to the UE project's Binaries/Win64 folder and I've also tried including one of utf8proc's subfolders, which has a few more h and c files, but nothing.

At my wits' end. Any help would be greatly appreciated.

Upvotes: 1

Views: 196

Answers (1)

john
john

Reputation: 88027

The clue here is the __imp in your unresolved names. This means that your compiler is telling the linker that you want to link dynamically. 'Imp' stands for import, and on the Windows platform dynamic linking is commonly done with an import library.

From looking at the utf8proc.h header file it seems that the solution is to add #define UTF8PROC_STATIC before including the header file.

#define UTF8PROC_STATIC
#include <utf8proc.h>

Alternatively you could use the compiler option to define this symbol.

I could not find this officially documented anywhere, but I expect it will work.

Upvotes: 2

Related Questions