문제

I'm expecting a lot of difficulties to make my program working with the library libmodbus on Linux.

I've installed libmodbus with the command sudo make install and after make but the problem is when I want to link the library in my C program.

My Makefile for now is like:

all: test
test: main.o com.o
  gcc main.o com.o -o test

main.o: main.c
    gcc -c main.c -o main.o

com.o: com.c
    gcc -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o

clean:
    rm -rf *o test

In my file com.c I include the file modbus.h like this:

#include <modbus.h>

And I always get the error:

fatal error: modbus.h: No such file or directory.

If it can help when I did make install, the code return me this:

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'
도움이 되었습니까?

해결책

It seems like the modbus.h is not in the standard include directory. You should to add the -I/<includes_path> flag to gcc options.

I suppose here:

gcc -I/<include_dir_path> -c com.c -Wl,-rpath=/usr/local/lib -Wl,LIBDIR -o com.o
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top