Fatal error: modbus.h: No such file or directory - c

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

Related

How to change the following Makefile of LINUX ,so that it works on MACOS as well [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a Makefile on my Linux Pc.I want to build and run the same program on MacOS too . How to edit the following Makefile ?
I want to compile a C program that has a header file of a graphics library EGGXProCALL JAXA "eggx.h".
I already installed Xcode ,XQuartz.
I have installed EGGX file on the following directory .
[Home#/opt/eggx]$
I have a C file in the following directory.
[Home#~/Desktop/development]$
I have checked the followings .
① gcc works fine here [Home#~/Desktop/development]$
② even the sample program to display a digital clock works ,
when the current directory is [Home#/opt/eggx]$
③ PROBLEM
when I tried to build [make] the program from [Home#~/Desktop/development]$ by using the makefile that I had in Linux.
I always get the following error message.
[Home~/Desktop/development]$ sudo make
gcc -c main.c
main.c:7:10: fatal error: 'eggx.h' file not found
#include <eggx.h>
^~~~~~~~
1 error generated.
make: *** [main.o] Error 1
[Home#~/Desktop/development]$
Here is the Makefile
# Makefile
OBJS = main
$(OBJS): $(OBJS).o
# gcc -O2 -Wall $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
gcc $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -c $(OBJS).c
.PHONY: clean
clean:
rm -f $(OBJS) $(OBJS).o
You must do things in the correct order and not proceed to the next step until you have correctly completed the previous step.
1. Download, and extract the library.
The download is normally done with git clone or scp to copy the source files from somewhere.
The extract (unpack from archive) is normally done with:
tar -xvf eggx-0.93r5.tar
That will normally create a new directory (with the same name as the tar-file but without the .tar extension) like:
eggx-0.93r5
2. Build the library.
Normally you need to change directory into the newly created one and run make. I gave you the Makefile last time so you need to do:
cd eggx-0.93r5
cp MAKEFILEFROMMARK Makefile
make
There should be no errors. If there are errors, you must solve them and then run:
make clean # delete any rubbish from previous failed build
make
3. Install the library.
You normally do this with:
make install
What that actually does depends on the package you are installing, but as a general rule, it will copy the header files and the libraries you just made into a "known" location, like /usr/local or /opt/package. The idea is to make all the files your own code will need available to all users of the computer by "publishing" or installing them to known locations.
4. Work out how to compile a simple C program that uses the library.
You should do the following steps in a completely different directory from where you downloaded the library to - do not mix your code with the library's code.
If your program uses eggx.h like this:
#include "eggx.h"
then you need to find where eggx.h is like this:
find /usr /opt /Users -name eggx.h
If that results in:
/path/to/somewhere/include/eggx.h
that means you must add this to your gcc command to tell the compiler how to find it:
gcc -I/path/to/somewhere/include ...
If your library is called libeggx.a, you need to find that too:
find /usr /opt /Users -name "libegg*a"
If that results in:
/path/to/somewhere/lib/libeggx.a
that means you need to add this to your gcc command to tell the linker where it is and what it is called:
gcc ... -L/path/to/somewhere/lib -leggx
If your program uses X11, you must install XQuartz on a Mac, and add the flags/switches for X11 into your compilation:
gcc ... -I /opt/X11/include -L /opt/X11/lib -lx11 ...
So, putting all that together, if your program is called program.c, you will compile and link with:
gcc program.c -o program -I/path/to/somewhere/include -I /opt/X11/include -L /opt/X11/lib -lx11 -L/path/to/somewhere/lib -leggx
and then run with:
./program
5. Make a Makefile that enshrines what you learned at (4).
That might look something like this:
EGGINC = -I /path/to/somewhere/include
EGGLIB = -L /path/to/somewhere/lib -leggx
X11INC = -I /opt/X11/include
X11LIB = -L /opt/X11/lib -lx11
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) $(EGGLIB) $(X11LIB)
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include $(EGGINC) $(X11INC) -c $(OBJS).c
You build your program in two steps:
Build the source file into an object file
Link the object file with libraries to create the final executable program
The preprocessor (which handles #include directives) is part of the building of the object file. So all flags that are needed for creation of the object files (like the -I option) should be present there and only there.
So the two rules could be changed as follows:
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include -c $(OBJS).c
Of course that assumes that the EGGX library was installed in /usr/local.

Makefile - Unix for C program

I am trying to make a makefile on linux. When i run "make" on my makefile it only says " 'mkFile' is up to date", where mkFile is the name of my makefile. I dont have any .o files or a compiled program prior to running the make command. I only have C files in the directory, so i dont know why it wont generate the executable and .o files. I have 5 files: Main.c, convertSentence.c, convertAll.c, convertSentence.h, and convertAll.h
Main.c includes convertAll.h, and convertAll.c includes convertAll.h.
Here is my code:
main: Main.o convertSentence.o convertAll.o
gcc -o program Main.o convertSentence.o convertAll.o
Main.o: Main.c convertAll.h
gcc -c Main.c
convertAll.o: convertAll.c convertSentence.h
gcc -c convertAll.c
convertSentence.o: convertSentence.c
gcc -c convertSentence.c
clean:
rm *.o
When i run "make" on my makefile it only says " 'mkFile' is up to date", where mkFile is the name of my makefile.
That indicates to me that you are using the following command line:
make mkFile
When you use that, it thinks it needs to build the target named mkFile.
You need to use:
make -f mkFile
When you use that, it indicates to make that the targets and dependencies for make are defined in the file named mkFile.
Another option is to rename mkFile to makefile or Makefile and then just use
make

Error compiling C code using MinGW-w64 in Windows 7

I'm trying to compile code from a backtrace project https://code.google.com/p/backtrace-mingw/ which is written for MinGW, but using MinGW-w64.
My old install and fresh install of MinGW-w64 produce the same problem. Path is set in path variables, and also in command prompt:
C:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32\bin
and C:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32 although this one isn't needed.
This is the makefile of that project:
.PHONY: all clean
all : backtrace.dll test.exe
backtrace.dll : backtrace.c
gcc -O2 -shared -Wall -o $# $^ -lbfd -lintl -liberty -limagehlp
test.exe : test.c
gcc -g -Wall -o $# $^
clean :
-del -f backtrace.dll test.exe
When compiling I get the warning:
backtrace.c:23:17: fatal error: bfd.h: No such file or directory #include < bfd.h>`
Which is weird because that file exists in ../mingw32/include folder.
If I add this when compilind the dll: -IC:\mingw-w64\i686-4.9.2-win32-sjlj-rt_v3-rev1\mingw32\include it continues but stops at the directive: #error config.h must be included before this header and config.h is missing in MinGW-w64
Any ideas?
That path is definetely missing from gcc include paths in mingw. I don't know why. You have to add it yourself in any way you like: cmake recipe, autoconf recipe, CFLAGS, CPATH, gcc specs.
And, as far as I remember, it uses only HAVE_STRINGIZE macro from config.h and it is used only to define CONCAT4 macro, that's not used anywhere in bfd.h. So, it's safe to cheat a little and put
#define PACKAGE package
before including bfd.h
add this to the end of the compile statement:
-I./mingw32/include
so the whole compile statement would be:
gcc -g -Wall -o $# $^ -I./mingw32/include
so the compiler knows where to find the include files

Can't load dll in linux gcc

I'm trying to compile my code with a DLL i made and I get the error below when i write ./prog
./prog: error while loading shared libraries: libctest.so.1: cannot open shared object file: No such file or directory
I followed the tutorial here and my mono app has no problem loading the dll and calling the functions. The key parts of the tutorial were
gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
My C# code does
[DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern void test();
I built with
gcc -Wall -L/opt/lib main.c -lctest -o prog
This is the first thing i changed. prog.c to main.c. From there I simply run with ./prog to get the error at the top of this question. I do this as root so there shouldn't be permission issues. I also chmod 755 the so's. What am I doing wrong? This is opensuse. I had to create the /opt/lib so I am thinking this path isn't registered where it should be
The dynamic linker ld.so will not look in /opt/lib by default when attempting to resolve library dependencies. You have to tell the linker about the non-standard library directories or add the /opt/lib path to your prog binary.
eg
LD_LIBRARY_PATH=/opt/lib ./prog
or, link prog with the -rpath linker option. This provides an additional path for the linker to search when resolving locations of shared objects.
gcc -Wall -L/opt/lib -Wl,-rpath,/opt/lib main.c -lctest -o prog
You can use the ldd command on a binary to show the locations of shared libraries.
ldd prog
This will currently show that libctest.so cannot be found. With an additional search path, the following command should show that all libraries are resolved:
LD_LIBRARY_PATH=/opt/lib ldd prog

Makefile to compile c/h?

I need to compile an old application whose tarball only contains *.c and *h, ie. no Makefile. The root directory contains the application, and a sub-directory contains a library the application needs.
My make/Makefile knowledge isn't great, and I was wondering what the easiest way would be to compile this application + library.
Thank you.
Edit: Using this script...
# cat compile.bash
#!/bin/bash
cd mylib
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
cd ..
gcc *.c -I mylib -L mylib -mylib -o myapp
... I notice that each *.c file in mylib/ is compiled into a *.so file instead of compiling each into an object file and building a single .so file, and I get tons of warnings and errors, eg.
unzip.c: In function âunzipâ:
unzip.c:991: warning: format not a string literal and no format arguments
gcc: unrecognized option '-mylib'
file_util.c: In function âfile_moveâ:
file_util.c:98: error: âerrnoâ undeclared (first use in this function)
I don't know how to compile the library, and then compile the application without error/warning.
No need to use a for loop or generate intermediate object files:
(cd mylib && gcc -shared -fPIC -o libfoo.so *.c) && \
gcc -Imylib -o app *.c mylib/libfoo.so
Compile the library:
cd libfoo
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
After this, you should have a libfoo.so file in libfoo/. Then, compile the program (Don't forget to cd back):
gcc *.c -I libfoo -L libfoo -lfoo -o application
The easiest is probably to get an IDE to do the build for you. Netbeans for one will create a Makefile so you can then build the project independently of the IDE.

Resources