1

Is it possible to compile Tesseract ORC as pure C without linking the C++ standard lib?

I compiled Tesseract following the instructions here, which worked fine. But when I linked it with the sample c program from the docs, it gave me the following errors:

  "std::__1::basic_streambuf<char, std::__1::char_traits<char> >::~basic_streambuf()", referenced from:
      std::__1::basic_filebuf<char, std::__1::char_traits<char> >::~basic_filebuf() in libtesseract.a(libtesseract_api_la-baseapi.o)
      std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf() in libtesseract.a(libtesseract_api_la-baseapi.o)
  "std::__1::cin", referenced from:
      tesseract::TessBaseAPI::ProcessPagesInternal(char const*, char const*, int, tesseract::TessResultRenderer*) in libtesseract.a(libtesseract_api_la-baseapi.o)

Obviously it's trying to link the C++ standard library. Is it possible to compile as pure c, without the C++ standard lib? I want to eventually cross compile for arm-7 without standard lib support.

Here is my makefile (And no, the answer is not turn gcc to g++. I want to compile as c):

CFLAGS = -c -Wall -I../src/api/ -I../src/ccstruct -I../src/ccutil -I../leptonica/src/ -I../leptonica/build/src

default: main

main.o: main.c
    gcc $(CFLAGS) -c main.c -o main.o

main: main.o
    gcc main.o ../leptonica/lib/nodebug/liblept.a ../src/api/.libs/libtesseract.a -o main

clean:
    -rm -f *.o
    -rm -f main
Community
  • 1
  • 1
Jason
  • 13,563
  • 15
  • 74
  • 125

1 Answers1

1

In general, C++ applications require/depend on the C++ standard library in several ways. Therefore, you should either provide the dependency or, if you really want to avoid it, statically link it. In addition, avoid statically linking several times the standard library (if several of your dependencies based on C++ require it).

Having said that, your next goal of avoiding entirely the C++ standard library is way harder. Not only you will have to provide alternative implementations of the C++ standard types & functions that Tesseract seems to use (or modify the library to avoid their use); but you will have to deal with several other issues: exceptions, RTTI, startup code & constructors, calls to special functions generated by the compiler like memcpy, linking to libgcc...

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • So Tesseract is only a C++ app and cannot be compiled in C with C++ standard lib? – Jason Apr 28 '18 at 18:41
  • @Jason: `libtesseract` exposes a C API that you can use from C or other languages that can call C functions. However, that does not have anything to do with the fact that `libtesseract` depends on other libraries (in this case, the C++ standard library). It seems you are confusing the stage of compiling `libtesseract` itself with the stage of compiling your own code that links to `libtesseract`. [...] – Acorn Apr 28 '18 at 20:21
  • @Jason [...] Take a look at questions like https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work and https://stackoverflow.com/questions/3322911/what-do-linkers-do/33690144 to try to understand the process better. – Acorn Apr 28 '18 at 20:21