Makefile to compile c/h? - c

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.

Related

How can I compile a header file and a C file together?

I created a file.h and a file.c how can I compile them on Ubuntu?
You only need to compile your .c file(s), not your .h file(s).
To compile file.c on Ubuntu, you can use GCC:
gcc file.c -o my_program
...or Clang:
clang file.c -o my_program
It is possible to precompile your header files, but you only need precompiled headers in particular cases. More information here.
If file.h is not in the same folder as file.c, you can use GCC or Clang's -I option.
Example if file.h is in the include/ folder:
gcc -I include/ file.c -o my_program
In file.c you still have this instruction, with only the filename:
#include "file.h"
You can also use a more generic approach by the usage of a makefile.
Here is a short example of such a file:
# Declaration of variables
CC = gcc
CC_FLAGS = -w -Werror -Wall
# File names
# "prgoram" will be the name of the output produced from the make process
EXEC = program
#Incorporates all the files with .c extension
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.c
$(CC) -c $(CC_FLAGS) $< -o $#
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
To use this utility just make sure that the file itself is within the directory containing your source files and its name is either "makefile" or "Makefile".
To compile the code simply run the following command from your working directory:
make program
This command will automatically link all the source files within your working directory into one executable file with the name of "program". To run the program itself just use the command:
./program
To clean your project and the created executable you can run the command:
make clean
The makefile is very powerful when dealing with larger projects that contain a larger number of source files. Here you can check for more guidance on how to use makefiles. This is also a very detailed tutorial on the topic.
Use following command to compile your program(For GCC Compiler):
gcc file.c -o file
No need to compile file.h file.

Dynamic linker gives "cannot open shared object file: No such file or directory" after using -L option

I have a project struct of a lib like this:
lib_ode/
src/
main/
c/
ode.c
incluce/
ode.h
test/
c/
test_ode.c
include/
target/
.objs/
test/
In this struct I had compile my project with those 4 commands:
(creating lib object)
gcc -fPIC -O3 -DNDEBUG -g -Wall -Isrc/main/include -c -o "target/.objs/ode.o" src/main/c/ode.c
(creating lib)
gcc -shared target/.objs/ode.o -lm -o "target/libode.so"
(creating test object)
gcc -fPIC -O3 -DNDEBUG -g -Wall -Isrc/main/include -c -Isrc/test/include -o target/.objs/test.o src/test/c/test_ode.c
(creating test)
gcc -L./target -lode target/.objs/test.o -o target/test/test.out
Then I tryed to run target/test/test.out and got this error.
target/test/test.out: error while loading shared libraries: libode.so: cannot open shared object file: No such file or directory
In my understand, -L option includes a folder for link, and after the second command I can find a file at target/libode.so.
So what am I doing wrong?
You have created an executable dynamically linked against a library in an arbitrary directory. At runtime, the dynamic linker cannot find the library (it doesn't exist in any of the standard search paths) and so gives you the error.
To run the program, you could set the LD_LIBRARY_PATH environment variable:
LD_LIBRARY_PATH="`pwd`/target" target/test/test.out
You could also trying linking with -rpath (I've not tried this):
gcc -L./target -Wl,-rpath,./target -lode target/.objs/test.o -o target/test/test.out
(The above should "hardcode" the additional dynamic library search path in the executable. If you later move the library you will get an error again. I'm not sure if the path stored in the executable will be relative or absolute; if it's relative, you'll need to keep the executable and library at the same path relative to each other).

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

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

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

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

Resources