0

I'm trying to use the libserial

File bitmap_test.cpp

#include <SerialStream.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace LibSerial;

int main()
{
   SerialStream serial_port("/dev/ttyS0");
   return 0;
}

The error:

In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:520: error: null character(s) ignored [-Werror]
In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:1: error: stray ‘\17’ in program
In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:525: error: null character(s) ignored [-Werror]
In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:1: error: stray ‘\231’ in program
In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:529: error: null character(s) ignored [-Werror]
/usr/local/lib/libserial.a:118:532: error: null character(s) ignored [-Werror]
In file included from <command-line>:0:0:
/usr/local/lib/libserial.a:118:1: error: stray ‘\10’ in program
/usr/local/lib/libserial.a:118:1: error: stray ‘\34’ in program
...

The makefile

COMPILER      = -c++
OPTIONS       = -ansi -pedantic-errors -Wall -Wall -Werror -Wextra -o
LINKER_OPT    = -L/usr/lib -lserial  -include/usr/local/lib/libserial.a -lstdc++ -lm

all: bitmap_test

bitmap_test: bitmap_test.cpp bitmap_image.hpp
    $(COMPILER) $(OPTIONS) bitmap_test bitmap_test.cpp $(LINKER_OPT)

I did research and understand it's a problem with encoding but don't know how to fix it. Any help is much appreciated.

The library is in the right path:

/sbin/ldconfig -p | grep libserial
    libserial.so.0 (libc6,x86-64) => /usr/local/lib/libserial.so.0
    libserial.so.0 (libc6,x86-64) => /usr/lib/libserial.so.0
    libserial.so (libc6,x86-64) => /usr/local/lib/libserial.so
    libserial.so (libc6,x86-64) => /usr/lib/libserial.so

But if I change the Makefile as suggested, I will have this problem:

c++ -ansi -pedantic-errors -Wall -Wall -Werror -Wextra -o bitmap_test bitmap_test.cpp -L/usr/local/lib -lstdc++ -lm -lserial
bitmap_test.cpp: In function ‘int main()’:
bitmap_test.cpp:32:40: error: call of overloaded ‘SerialStream(const char [11])’ is ambiguous
 SerialStream serial_port( "/dev/ttyS0" ) ;
                                        ^
bitmap_test.cpp:32:40: note: candidates are:
In file included from bitmap_test.cpp:19:0:
/usr/local/include/SerialStream.h:98:13: note: LibSerial::SerialStream::SerialStream(std::string, LibSerial::SerialStreamBuf::BaudRateEnum, LibSerial::SerialStreamBuf::CharSizeEnum, LibSerial::SerialStreamBuf::ParityEnum, short int, LibSerial::SerialStreamBuf::FlowControlEnum)
             SerialStream( const std::string fileName,
             ^
/usr/local/include/SerialStream.h:84:22: note: LibSerial::SerialStream::SerialStream(std::string, std::ios_base::openmode)
             explicit SerialStream( const std::string fileName,
                      ^
make: [bitmap_test] Error 1 (ignored)
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Hakeem Saad
  • 11
  • 1
  • 2
  • 2
    What should the option `-include/usr/local/lib/libserial.a` actually do? I'm pretty sure this is the culprit. – πάντα ῥεῖ Jul 05 '16 at 21:57
  • The makefile generated by eclipse shows that option, as I include files from C/C++ Build/Settings/Tool Settings/Includes – Hakeem Saad Jul 05 '16 at 22:05
  • i figured it out i just change the include inside make file to this -L/usr/local/lib/libserial.la – Hakeem Saad Jul 05 '16 at 22:45
  • Here is [a similar question](https://stackoverflow.com/questions/28759855/daemon-on-embedded-linux-device-using-busybox-be-written-in-c-or-as-a-script) (where the compiler gets to chew on binary files). See also its *"Linked"* questions. For example, [this one](https://stackoverflow.com/questions/73410284/why-do-i-get-weird-stray-errors-with-file-cxxopts-hpp-and-meson-ninja-build?noredirect=1&lq=1) where the build/make system is involved. – Peter Mortensen May 03 '23 at 21:32
  • Including a (binary) library as if it is a header file must be a duplicate. What is the canonical question? – Peter Mortensen May 21 '23 at 18:36

1 Answers1

2

Stray characters usually has to do with the source encoding.

The “null character(s) ignored” diagnostic would generally indicate that the source code is UTF-16 or UTF-32.

However, the makefile shows that you're treating a static library as a forced include file:

-include/usr/local/lib/libserial.a

With my g++ in Windows:

> g++ -v --help 2>&1 | find "-include "
  --include                   This switch lacks documentation
  -include <file>             Include the contents of <file> before other 
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thanks for your reply. could you please tell me how should i include the .a lib ? As you see, i tried -lserial also, and in /usr/local/lib, i have both libserial.a and libserial.so, but it does not work – Hakeem Saad Jul 05 '16 at 22:02
  • Hopefully `-lserial` will do the trick. Note that prefix `lib` and filename extension `.a` is removed. The linker adds that as it searches. – Cheers and hth. - Alf Jul 05 '16 at 22:03
  • @HakeemSaad, The only thing I can think of then is putting `-lserial` at the end of the command. That sometimes helps with linking issues. You can also refer to http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix. – chris Jul 05 '16 at 22:03
  • @HakeemSaad How to specify libraries to eclipse is covered here: http://stackoverflow.com/questions/1351712/how-to-add-a-library-to-an-eclipse-project. Helpful pictures here: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_general_pns_libpath.htm – user4581301 Jul 05 '16 at 22:29