DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.76k stars 389 forks source link

[win & VS] error LNK2019: unresolved external symbol LeptMsgSeverity referenced in function main #575

Closed zdenop closed 3 years ago

zdenop commented 3 years ago

I am not sure if I do something wrong but when I tried to compile e.g. pixafileinfo with:

cl /Fe pixafileinfo.c /If:\win64\include\leptonica /link /LIBPATH:f:/win64/lib leptonica-1.81.0.lib /machine:x64

I got error:

Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29914 for x64 Copyright (C) Microsoft Corporation. All rights reserved.

pixafileinfo.c
Microsoft (R) Incremental Linker Version 14.28.29914.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:pixafileinfo.exe
/LIBPATH:f:/win64/lib
leptonica-1.81.0.lib
/machine:x64
pixafileinfo.obj
pixafileinfo.obj : error LNK2019: unresolved external symbol LeptMsgSeverity referenced in function main
pixafileinfo.exe : fatal error LNK1120: 1 unresolved externals

If I replace return ERROR_INT(...) with

    fprintf(stderr,... );
    return 1;

Compilation is successful and program (pixafileinfo.exe) works.

I use the latest leptonica build with cmake and command:

cmake -Bbuild -DCMAKE_BUILD_TYPE=Release ^
   -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_PREFIX_PATH=%INSTALL_DIR% ^
   -DBUILD_PROG=OFF -DSW_BUILD=OFF -DBUILD_SHARED_LIBS=ON

cmake --build build --config Release --target install

Any idea what could be wrong?

DanBloomberg commented 3 years ago

That is very strange. The global LeptMsgSeverity is defined in utils1.c, and it is declared and used in ERROR_INT in environ.h.

If you are linking the library, it includes these two files. Does it matter if the .h file occurs before the library on your compile/link command at the top?

DanBloomberg commented 3 years ago

Also, did this only happen for pixafileinfo?

DanBloomberg commented 3 years ago

Also, does it give the error when you do static linking instead of shared?

zdenop commented 3 years ago

When I build leptonica (AFAIK as static) this way: cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR% -DCMAKE_PREFIX_PATH=%INSTALL_DIR% -DSW_BUILD=OFF -DBUILD_PROG=ON it works (and it built other leptonica programs).

Also I found out that when I add LEPT_DLL l_int32 LeptMsgSeverity; before main function (in pixafileinfo.c) compilation/linking works with also with shared library .

zdenop commented 3 years ago

Actually problem for me is not pixafileinfo.c build. I tried to build and test one utility on windows that uses leptonica and its build failed with the same error, so I come back to test build of leptonica program by hand to find reason....

zdenop commented 3 years ago

OK. I solved it: adding /D LIBLEPT_IMPORTS /TP made a magic:

cl /If:\win64\include\leptonica /D LIBLEPT_IMPORTS /TP pixafileinfo.c /link /LIBPATH:f:/win64/lib leptonica-1.81.0.lib

I found it when I look at details why cmake was able to build progs with shared libs ( -DSW_BUILD=OFF -DBUILD_PROG=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_VERBOSE_MAKEFILE=ON)

DanBloomberg commented 3 years ago

Thank you for solving the problem -- I knew it was above my pay grade.

Do you think something should be added to the build setup or instructions, or are you using a method of building that is non-standard? (Hint: nobody has reported this problem before you).

zdenop commented 3 years ago

I do not know. If somebody builds progs with cmake, there is no problem. Problem is "only" on windows with VS (or clang-cl), so information in this issue should be sufficient.

GitGoliath commented 1 year ago

Adding comment for other developers using only Visual Studio (VS) as building tool (no CMake or command line).

I had same issue building from new VS and including code from skewtest.c and using leptonica-1.84.0d.lib in a test program I made.

Adding LIBLEPT_IMPORTS to preprocessor directive in my program was helpful, it resolve the LeptMsgSeverity not found.

But the other issues afterward were the missing functions for all the functions from Leptonica. The use of /TP is good advice too, as above example is using command line for "cl", it can be confusing as VS does not permit to add file after /TP option enable in GUI. So for those, that use GUI, activate the /TP option on the Leptonica project since it is the leptonica-1.84.0d.lib that need to be C++ compatible when imported from a C++ project. This resolve my missing function in linker step as now the function were C++ named.

Hope this help other.