LoginSignup
2
1

More than 1 year has passed since last update.

libmodbusの導入手順

Posted at

前書き

libmodbusを導入した際のメモです。記載した内容で導入できました。
本記事を参考に実施した際に発生した損失や損害について一切の責任を負いません。

公式HP

導入環境

OS:Debian 9
Kernel:4.4~~だった気がする

今回使用したlibmodbusのVersion

3.1.6
公式HPから使用したいlibmodbusをDLする

DLしたlibmodbus-3.1.6.tar.gzを解凍する

README.mdを読む

README.md
Installation
You will only need to install automake, autoconf, libtool and a C compiler (gcc
or clang) to compile the library and asciidoc and xmlto to generate the
documentation (optional).

To install, just run the usual dance, `./configure && make install`. Run
`./autogen.sh` first to generate the `configure` script if required.

You have to check that the installation library path is
properly set up on your system (*/etc/ld.so.conf.d*) and library cache is up to
date (run `ldconfig` as root if required).

The library provides a *libmodbus.pc* file to use with `pkg-config` to ease your
program compilation and linking.

automakeとautoconfとlibtoolとC compiler(gcc)が必要とのこと
(Autoconfは、GNUm4とPerlが必要。m4とperlの必須最低VersionはAutoconfのReadmeを確認する)
Make、GCC、Perlは既にインストールされていたので割愛

GNUm4インストール手順コマンド

実行コマンド
cd /usr/local/src
wget http://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.gz
tar zxvf m4-1.4.18.tar.gz
cd m4-1.4.18
./configure
make
make check
make install

make checkは、ビルドできたことを確認するためのコマンド。(時間かかるからやらなくてもいいかも)

makeでコンパイルエラーが発生した

発生エラーログ
      CC       fflush.o
      CC       fpurge.o
      CC       freadahead.o
    freadahead.c: In function 'freadahead':
    freadahead.c:92:3: error: #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
      #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
       ^~~~~
    Makefile:1910: recipe for target 'freadahead.o' failed
    make[3]: *** [freadahead.o] Error 1
    make[3]: Leaving directory '/usr/local/src/m4-1.4.18/lib'
    Makefile:1674: recipe for target 'all' failed
    make[2]: *** [all] Error 2
    make[2]: Leaving directory '/usr/local/src/m4-1.4.18/lib'
    Makefile:1572: recipe for target 'all-recursive' failed
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory '/usr/local/src/m4-1.4.18'
    Makefile:1528: ターゲット 'all' のレシピで失敗しました
    make: *** [all] エラー 2

エラー対応

/usr/local/src/m4-1.4.18/lib配下のソースに対して変更を加える
参考にしたURL
https://askubuntu.com/questions/1099392/compilation-of-m4-1-4-10-to-1-4-18-fails-due-to-please-port-gnulib-freadahead-c
変更内容のURL
http://git.openembedded.org/openembedded-core/tree/meta/recipes-devtools/m4/m4/m4-1.4.18-glibc-change-work-around.patch

変更を加えた後再度makeでビルドできた

Automakeインストール手順コマンド

実行コマンド
cd /usr/local/src
wget http://ftp.gnu.org/gnu/automake/automake-1.16.3.tar.gz
tar xvfz automake-1.16.3.tar.gz
cd automake-1.16.3
./configure
make
make check
make install

Autoconfインストール手順コマンド

実行コマンド
cd /usr/local/src
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.71.tar.gz
tar xvfz autoconf-2.71.tar.gz
cd autoconf-2.71
./configure
make
make check
make install

libtoolインストール手順コマンド

実行コマンド
cd /usr/local/src
wget http://ftp.jaist.ac.jp/pub/GNU/libtool/libtool-2.4.6.tar.gz
tar xvfz libtool-2.4.6.tar.gz
cd libtool-2.4.6
./configure
make
make check
make install

autogen.shの実行

実行コマンド&ログ
/home/user/libmodbus-3.1.6# ./autogen.sh
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'build-aux'.
libtoolize: linking file 'build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: linking file 'm4/libtool.m4'
libtoolize: linking file 'm4/ltoptions.m4'
libtoolize: linking file 'm4/ltsugar.m4'
libtoolize: linking file 'm4/ltversion.m4'
libtoolize: linking file 'm4/lt~obsolete.m4'
configure.ac:33: warning: The macro `AC_PROG_CC_STDC' is obsolete.
configure.ac:33: You should run autoupdate.
./lib/autoconf/c.m4:1666: AC_PROG_CC_STDC is expanded from...
configure.ac:33: the top level
configure.ac:33: installing 'build-aux/compile'
configure.ac:56: installing 'build-aux/config.guess'
configure.ac:56: installing 'build-aux/config.sub'
configure.ac:32: installing 'build-aux/install-sh'
configure.ac:32: installing 'build-aux/missing'
src/Makefile.am: installing 'build-aux/depcomp'
parallel-tests: installing 'build-aux/test-driver'

------------------------------------------------------
Initialized build system. You can now run ./configure
------------------------------------------------------

configureの実行

実行コマンド&ログ
/home/user/libmodbus-3.1.6# ll conf*
-rw-r--r-- 1 root   root     8585  5月 14 09:13 config.h.in
-rwxr-xr-x 1 root   root   640843  5月 14 09:13 configure
-rw-r--r-- 1 user user   4912  4月 27 10:18 configure.ac
/home/user/libmodbus-3.1.6# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking how to create a pax tar archive... gnutar
checking whether make supports the include directive... yes (GNU style)
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for wchar.h... yes
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking whether _XOPEN_SOURCE should be defined... no
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... no
checking whether make supports nested variables... (cached) yes
checking build system type... aarch64-unknown-linux-gnu
checking host system type... aarch64-unknown-linux-gnu
checking how to print strings... printf
checking for a sed that does not truncate output... /bin/sed
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for fgrep... /bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert aarch64-unknown-linux-gnu file names to aarch64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert aarch64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for mt... mt
checking if mt is a manifest tool... no
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking for arpa/inet.h... yes
checking for byteswap.h... yes
checking for errno.h... yes
checking for fcntl.h... yes
checking for limits.h... yes
checking for linux/serial.h... yes
checking for netdb.h... yes
checking for netinet/in.h... yes
checking for netinet/tcp.h... yes
checking for sys/ioctl.h... yes
checking for sys/params.h... no
checking for sys/socket.h... yes
checking for sys/time.h... yes
checking for sys/types.h... (cached) yes
checking for termios.h... yes
checking for time.h... yes
checking for unistd.h... (cached) yes
checking for asciidoc... no
checking for xmlto... no
checking whether to build documentation... no
configure: WARNING: The tools to build the documentation aren't installed
checking for gcc options needed to detect all undeclared functions... none needed
checking whether __CYGWIN__ is declared... no
checking for accept4... yes
checking for getaddrinfo... yes
checking for gettimeofday... yes
checking for inet_ntoa... yes
checking for select... yes
checking for socket... yes
checking for strerror... yes
checking for strlcpy... no
checking for inline... inline
checking for g++... g++
checking whether the compiler supports GNU C++... yes
checking whether g++ accepts -g... yes
checking for g++ option to enable C++11 features... none needed
checking dependency style of g++... gcc3
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking for int64_t... yes
checking for size_t... yes
checking for ssize_t... yes
checking for uint16_t... yes
checking for uint32_t... yes
checking for uint8_t... yes
checking for winsock2.h... no
checking whether TIOCSRS485 is declared... yes
checking whether TIOCM_RTS is declared... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/modbus-version.h
config.status: creating src/win32/modbus.dll.manifest
config.status: creating tests/Makefile
config.status: creating doc/Makefile
config.status: creating libmodbus.pc
config.status: creating config.h
config.status: creating tests/unit-test.h
config.status: executing depfiles commands
config.status: executing libtool commands

        libmodbus 3.1.6
        ===============

        prefix:                 /usr/local
        sysconfdir:             ${prefix}/etc
        libdir:                 ${exec_prefix}/lib
        includedir:             ${prefix}/include

        compiler:               gcc
        cflags:                 -g -O2
        ldflags:

        documentation:          no
        tests:                  yes

/home/user/libmodbus-3.1.6#

make installの実行

実行コマンド&ログ
/home/user/libmodbus-3.1.6# make install
Making install in src
  CC       modbus.lo
  CC       modbus-data.lo
  CC       modbus-rtu.lo
  CC       modbus-tcp.lo
  CCLD     libmodbus.la
 /bin/mkdir -p '/usr/local/lib'
 /bin/bash ../libtool   --mode=install /usr/bin/install -c   libmodbus.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libmodbus.so.5.1.0 /usr/local/lib/libmodbus.so.5.1.0
libtool: install: (cd /usr/local/lib && { ln -s -f libmodbus.so.5.1.0 libmodbus.so.5 || { rm -f libmodbus.so.5 && ln -s libmodbus.so.5.1.0 libmodbus.so.5; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libmodbus.so.5.1.0 libmodbus.so || { rm -f libmodbus.so && ln -s libmodbus.so.5.1.0 libmodbus.so; }; })
libtool: install: /usr/bin/install -c .libs/libmodbus.lai /usr/local/lib/libmodbus.la
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
 /bin/mkdir -p '/usr/local/include/modbus'
 /usr/bin/install -c -m 644 modbus.h modbus-version.h modbus-rtu.h modbus-tcp.h '/usr/local/include/modbus'
Making install in doc
make[2]: 'install-exec-am' に対して行うべき事はありません.
Making install in tests
  CC       bandwidth-server-one.o
  CCLD     bandwidth-server-one
  CC       bandwidth-server-many-up.o
  CCLD     bandwidth-server-many-up
  CC       bandwidth-client.o
  CCLD     bandwidth-client
  CC       random-test-server.o
  CCLD     random-test-server
  CC       random-test-client.o
  CCLD     random-test-client
  CC       unit-test-server.o
  CCLD     unit-test-server
  CC       unit-test-client.o
  CCLD     unit-test-client
  CC       version.o
  CCLD     version
make[2]: 'install-exec-am' に対して行うべき事はありません.
make[2]: 'install-data-am' に対して行うべき事はありません.
make[2]: 'install-exec-am' に対して行うべき事はありません.
 /bin/mkdir -p '/usr/local/share/doc/libmodbus'
 /usr/bin/install -c -m 644 MIGRATION README.md '/usr/local/share/doc/libmodbus'
 /bin/mkdir -p '/usr/local/lib/pkgconfig'
 /usr/bin/install -c -m 644 libmodbus.pc '/usr/local/lib/pkgconfig'
/home/user/libmodbus-3.1.6#

色々配置されることを確認

実行コマンド&ログ
/home/user/source# find / -name "*modbus*"
/usr/local/share/doc/libmodbus
/usr/local/lib/libmodbus.la
/usr/local/lib/libmodbus.so.5
/usr/local/lib/pkgconfig/libmodbus.pc
/usr/local/lib/libmodbus.so
/usr/local/lib/libmodbus.so.5.1.0
/usr/local/include/modbus
/usr/local/include/modbus/modbus.h
/usr/local/include/modbus/modbus-tcp.h
/usr/local/include/modbus/modbus-version.h
/usr/local/include/modbus/modbus-rtu.h

ldconfigコマンドで共有ライブラリの場所を追加

実行コマンド&ログ
/home/user/source# ldconfig -p | grep modbus
/home/user/source# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
/home/user/source#vim /etc/ld.so.conf.d/modbus.conf
/usr/local/lib/

/home/user/source# ldconfig
/home/user/source# ldconfig -p | grep modbus
        libmodbus.so.5 (libc6,AArch64) => /usr/local/lib/libmodbus.so.5
        libmodbus.so (libc6,AArch64) => /usr/local/lib/libmodbus.so
/home/user/source#

導入はここで完了

libmodbusのソースを改造して再ビルドしたい場合

実行コマンド
/home/user/libmodbus-3.1.6# make clean
/home/user/libmodbus-3.1.6# make install

自作プログラムのビルド時Makefileでpkg-configを使用する場合

環境変数PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/を追加する

実行コマンド&ログ
/home/user/source# export -p | grep PKG_*
/home/user/source# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
/home/user/source# export -p | grep PKG_*
declare -x PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
/home/user/source#

Makefileの例

Makefile
CC = gcc
INCDIR = `pkg-config libmodbus --cflags`
CFLAGS = -O2 -Wall
#CFLAGS = -g -O2 -Wall $(INCDIR)
#DEST = /usr/local/bin
#LDFLAGS = -L/usr/local/lib
LIBS = `pkg-config libmodbus --libs`
OBJS = main.o hoge.o
PROGRAM = PROG_NAME

#GDB用オプションを有効にするならCFLAGの-O2を-O0に変更して最適化を無効にしたほうがよい
GDB = -g3

all: $(PROGRAM)

$(PROGRAM): $(OBJS)
        $(CC) $(CFLAGS) $(OBJS) $(LIBS) $(GDB) -o $(PROGRAM)
#       $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LIBS) -o $(PROGRAM)

main.o: main.c
        $(CC) $(INCDIR) -c main.c

hoge.o: hoge.c
        $(CC) $(INCDIR) -c hoge.c

clean:
        rm -f *.o *~ $(PROGRAM)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1