Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The 'main()' function in svpaint.cpp is overriding my main function #3865

Closed
namniav opened this issue Jul 10, 2022 · 8 comments
Closed

The 'main()' function in svpaint.cpp is overriding my main function #3865

namniav opened this issue Jul 10, 2022 · 8 comments
Labels

Comments

@namniav
Copy link

namniav commented Jul 10, 2022

Environment

  • Tesseract Version: 4.1.1, installed with vcpkg
  • Commit Number: unknown
  • Platform: Windows 10 21H2, 19044.1766, 64 bit

Current Behavior:

// main.cpp
#include <cstdio>
int main(int, const char*[]) {
  std::puts("hello tesseract");
  return 0;
}
$ g++ -L /k/vcpkg/installed/x64-windows/lib -ltesseract41 -lleptonica-1.82.0 main.cpp
K:/Qt6/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
  C:\Users\namniav\AppData\Local\Temp\ccSM394z.o:main.cpp:(.text+0x0): multiple definition of `main';
  K:/vcpkg/installed/x64-windows/lib/tesseract41.lib(tesseract41.dll):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

This example reports linkage error because tesseract41.dll is linked before main.o. But when it is linked to a Qt&CMake project, it is overriding my main function silently and writes Starting java -Xms512m -Xmx1024m -Djava.library.path=. -jar ./ScrollView.jar to log.

Expected Behavior:

I think shared library should not carry a main function.

@zdenop
Copy link
Contributor

zdenop commented Jul 10, 2022

tesseract 4.x is the unsupported version. Please use the recent tesseract version (5.2).
I tried clang:

clang-cl main.cpp -o test.exe -I F:/win64/include F:/win64/lib/tesseract51.lib

and there was no problem.
I tried VS:

cl main.cpp -I F:/win64/include /link /LIBPATH:F:\win64\lib tesseract51.lib

and there is no problem.
I tried MSYS2 (with the default installation of tesseract 5.1):

@level MINGW64 /F/Projects/Community/tesseract
# g++ main.cpp -I /clang64/include/ -L /clang64/lib/ -l tesseract

Actually, tesseract executable (also 4.x version) has the main function and links to link tesseract library without a problem on any platform...

int main(int argc, char** argv) {

@namniav
Copy link
Author

namniav commented Jul 10, 2022

@zdenop
Currently 4.1.1 is the only version supported by vcpkg. It is the easiest and stable way to build tesseract. If the issue was fixed in 5.x, I can try building a newer version.

But all your tries were linking tesseract AFTER main.o(or main.cpp), can you try linking tesseract FIRST? This is the default behavior when using CMake: when A depends B, B is linked before A itself otherwise there will be undefined references in A if it is referencing symbols from B. But MSVC's linking algorithm is different and order insensitive when linking shared libraries, if I recall correctly. Anyway, when linking/loading multiple shared libraries, symbols with same names from later libraries are ignored.

@zdenop
Copy link
Contributor

zdenop commented Jul 10, 2022

This is the default behavior when using CMake:

Maybe I am missing something but our cmake builds works:

E.g. https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_18.html (but also SO ) claims that:

$ gcc -Wall calc.c -lm -o calc   (correct order)
$ gcc -Wall -lm calc.c -o calc    (incorrect order)

so your propopsed compilation order is not correct.

@namniav
Copy link
Author

namniav commented Jul 10, 2022

Sorry that I messed up some memories about linkers... But the problem still exists and I have found the cause:

int main(int argc, char** argv) {

This file is included by
src/viewer/*.cpp

I fed this patch to vcpkg and rebuilt tesseract, then the problem is fixed.

-add_library                     (libtesseract ${LIBRARY_TYPE} ${tesseract_src} ${arch_files}
+set(libtesseract_src ${tesseract_src})
+list(REMOVE_ITEM libtesseract_src
+                 ${CMAKE_CURRENT_SOURCE_DIR}/src/viewer/svpaint.cpp)
+
+add_library                     (libtesseract ${LIBRARY_TYPE} ${libtesseract_src} ${arch_files}
$ nm installed/x64-windows/lib/tesseract41.lib | grep "main"
... # no main function reported

My Qt&CMake project works fine now.

Also I guess the similar problem exists for tesseract executable target, since there are two main functions, one from src/api/tesseractmain.cpp and the other from src/viewer/svpaint.cpp. I have no idea how they could work together.

@amitdo amitdo changed the title Why is there a "main" function in tesseract41.dll? It is overriding my main function. The 'main()' function in svpaint.cpp is overriding my main function Jul 11, 2022
@amitdo amitdo added the bug label Jul 12, 2022
@amitdo
Copy link
Collaborator

amitdo commented Jul 12, 2022

svpaint.cpp seems to be a utility for testing ScrollView. It should not be compiled as part of libtesseract.

@amitdo amitdo removed the bug label Jul 12, 2022
@amitdo
Copy link
Collaborator

amitdo commented Jul 12, 2022

The Autotools and sw build definitions do not contain an instruction to compile this file.

CMakeLists.txt does contain an instruction to compile it.

(

src/viewer/*.cpp

@amitdo amitdo added the bug label Jul 12, 2022
@amitdo
Copy link
Collaborator

amitdo commented Jul 15, 2022

An alternative to the suggested changes is to move svpaint.cpp to a subdirectory: viewer/test. Another one is to explicitly list file names instead of using *.cpp.

@stweil, do you have a preference?

@stweil
Copy link
Member

stweil commented Jul 18, 2022

Pull request #3873 should fix this issue. It moves src/viewer/svpaint.cpp to src/svpaint.cpp.

I also considered moving it to unittest, but think that the current solution is simpler.

@amitdo amitdo closed this as completed Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants