1

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'
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JoSav
  • 247
  • 2
  • 6
  • 19
  • See [Compiling a progam that includes `libmodbus` in C](http://stackoverflow.com/questions/22697543/compiling-a-program-that-includes-libmodbus-in-c) when you get past the hurdles of creating the `main.o` and `com.o` files. – Jonathan Leffler May 13 '14 at 04:46

1 Answers1

2

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
Alex
  • 9,891
  • 11
  • 53
  • 87
  • The problem is that even if do a manual include like -I on the source code I get another error like main.o file not recognized.And I get collect2: ld returned 1 exit status – JoSav May 12 '14 at 13:10
  • It's not that I have another error, it's because I already tried this solution and on google they said that I have to link the makefile with the librairy, not the source code so "-I – JoSav May 12 '14 at 13:31
  • Ok, but the described in question error is totally preprocessing error and not a linkage error. What error do you have if you give a full path to the header? – Alex May 12 '14 at 18:51
  • 1
    The `-Wl,` options are not material to creating an object file (`com.o`). They are directives to the linker, which is run when creating the executable. They'll be needed in the line which currently reads `gcc main.o com.o -o test`. The `-I` suggestion is 100% correct for creating the object files, though. – Jonathan Leffler May 13 '14 at 04:43