GCC compile with mysql library - c

when I try to compile a self written project in C with includes the mysql libraries I get this error:
gcc -c src/oDAO.c
src/oDAO.c:4:23: fatal error: my_global.h: No such file or directory
I included the my_global.h as following:
#include <my_global.h>
The error comes up, because my system copied the header files to /usr/include/mysql/ and gcc is searching for system header files only in /usr/include (without subdirectories). How can I call gcc with adding /usr/include/mysql as additional shared library root?
Here is my acutal Makefile:
all: main.o oDAO.o FileUtils.o DVDDAO.o
gcc -Llib -o oDAO main.o oDAO.o FileUtils.o DVDDAO.o -llinkedlist -lncurses `mysql_config --cflags --libs`
main.o:
gcc -c src/main.c
oDAO.o:
gcc -c src/oDAO.c
FileUtils.o:
gcc -c src/FileUtils.c
DVDDAO.o:
gcc -c src/DVDDAO.c
clean:
rm -f *.o
rm -f oDAO
rm -f *.bak
rm -f *.~

I've got the answer on my own:
Everytime i try to compile a .c file that includes a mysql library into an object file I have to compile it like that:
gcc -c DVDDAO.c `mysql_config --cflags`
Then everything works fine

Try the option -I for adding a include directory and -L for adding a libary directory.

Related

Writing a make file for GLFW3

i'm writing an emulator program and I need a graphics library. I have 4 files, the graphics library GLWF3 is installed in my includes folder. Im using MacOs Yosemite. I can't figure out how to get the makefile working though to include the glfw3 library. Thanks in advance!
Also note the only file including GLWF3 is graphics.h
Makefile
OBJ = graphics.o chip8.o
emulator: $(OBJ)
gcc -o emulator $(OBJ)
graphics.o: graphics.c graphics.h
gcc -c graphics.c
chip8.o: chip8.c chip8.h
gcc -c chip8.c
clean:
rm -f $(OBJ) emulator
To build with a given library, you have to:
tell the compiler how to find library header file
tell the linker which what library at must link.
Compilation
To tell where are the headers, you must pass a -I/path/to/dir option to gcc. Often, the make CFLAGS variable is used to do so:
CFLAGS= -I/path/to/glfw/include/dir
graphics.o: graphics.c graphics.h
gcc -c graphics.c $(CFLAGS)
chip8.o: chip8.c chip8.h
gcc -c chip8.c
Link
To tell linker what library to use, and where it is located, option -L/path/to/sofile and -lthelib are used. Usually in LDFLAGS variable:
Warning: The -l options must come after the files to link (*.o files)
LDFLAGS = -L/path/to/libglfw/lib/dir
# if the so file name is "libglfw3.so", the "-l" option must be "-lglfw3"
LDFLAGS += -lglfw3
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
pkg-config
To not to have to deal with paths, you can use pkg-config tool: This tool will help you to set CFLAGS and LDFLAGS variables. See here for installation instructions..
Hence, you makefile will looks like:
OBJ = graphics.o chip8.o
# calling program "pkg-config" and store result in CFLAGS variable
CFLAGS = $(shell pkg-config --cflags glfw3)
# calling program "pkg-config" and store result in LDFLAGS variable
LDFLAGS = $(shell pkg-config --ldflags glfw3)
emulator: $(OBJ)
gcc -o emulator $(OBJ) $(LDFLAGS)
graphics.o: graphics.c graphics.h
gcc $(CFLAGS) -c graphics.c
chip8.o: chip8.c chip8.h
gcc $(CFLAGS) -c chip8.c
clean:
rm -f $(OBJ) emulator

dynamic library gcc compilation error

I have the following code:
gcc -Wall -fno-stack-protector -O2 -g -fPIC -c ec.c
pwd
gcc -shared -Wl,-soname,libec.so.1 -o libec.so.1.0 ec.o /urs/src/soem/ethercat*.o ../soem/nicdrv.o -lc -lpthread
mv libec.so.1.0 /usr/lib/.
cd /usr/lib
ldconfig -v -n
ln -sf libec.so.1.0 libec.so
ln -sf libec.so.1.0 libec.so.1
It gives the following error when compiling:
/home/ebox/Documents/SVN/Libs/ec
gcc: error: /urs/src/soem/ethercat*.o: No such file or directory
mv: cannot stat ‘libec.so.1.0’: No such file or directory
I understand there is something wrong with the gcc command, but cannot figure out how to fix this. There are several .o files in the path that start with ethercat*.
How can I get this fixed?
The error means that there are no files that match the pattern /urs/src/soem/ethercat*.o.
Note that the first component is urs. Probably it should be usr.

How can I write Makefile (with sub Makfile ) more concise

When I do practice , I have a practice path.
Under this path , I have an Include path named myInclude (I have some useful function is this folder and I always use it.)
And a code path named symbol_try.I always make add new folder (with a c file and main function in it) in symbol_try and compile it.
Each time I have to compile it by gcc in terminal .Its a boring work , so I write a Makefile.
Here is an example:
the main Makefile in practice path:
FOBJS=
include myInclude/Rule.mk
include symbol_try/codeList_13.1/Rule.mk
symbol:$(FOBJS) <==What exactly I what . A executable file.
gcc -o symbol $(FOBJS) -pthread -lpthread
subsystem:
cd myInclude/ && $(MAKE)
cd symbol_try/codeList_13.1/ &&$(MAKE)
clean:
rm -rf symbol
In the myInclude/Rule.mk
FOBJS+=myInclude/otherFunction.o myInclude/error.o \
myInclude/unit.o myInclude/unitTest.o\
In the symbol_try/codeList_13.1/Rule.mk
FOBJS+=symbol_try/codeList_13.1/codeList_13.1.o
In myInclude/Makefile:
OBJS=otherFunction.o error.o unit.o unitTest.o
ALL:$(OBJS)
.PHONY:ALL
$(OBJS):%.o:%.c
gcc -c $< -o $#
clean :
otherFunction.o error.o unit.o
In symbol_try/codeList_13.1/Makefile:
codeList_13.1.o:codeList_13.1.c
gcc -c codeList_13.1.c
Well.That can work. But as you see , I have to write a Rule.mk(to initialize the FOBJS) and a Makefile for each folder.
I am new for make , I want find a way more concise , witch I only need write one Makefile for each folder and a main Makefile.No Rule.mk any more.
PS: I always change the code in myInclude ,so I don't want to build it a library.
Thanks for any help.
Here's one way you can do it with just one Makefile:
CC = gcc
CPPFLAGS += -I myInclude/ (1)
CFLAGS += -std=c99 -Wall (2)
VPATH = myInclude/ \ (3)
symbol_try/codeList_13.1/
symbol: otherFunction.o error.o unit.o unitTest.o codeList_13.1.o (4)
$(CC) -o $# $^ (5)
.PHONY : clean
clean:
rm -f symbol *.o
Note that make knows how to build C files and has some standard macros: CC, CPPFLGAS, CFLAGS
Add the include paths of your headers. You presumably have some headers for the individual object files in the myInclude directory.
Put the compiler flags here.
Add the paths to the source files you want to build.
List the object files that the executable depends upon
As there is no file called symbol.c you need to tell make how to create symbol.o with a rule. $# means the target ('symbol', here), and $^ means all of the prerequisites (the object files listed).
Here's a list of all of the files in my test directories for this:
$ find . -type f
.
./Makefile
./myInclude/error.c
./myInclude/header.h
./myInclude/otherFunction.c
./myInclude/unit.c
./myInclude/unitTest.c
./symbol_try/codeList_13.1/codeList_13.1.c
And the build output:
$ make
gcc -std=c99 -Wall -I myInclude/ -c -o otherFunction.o myInclude/otherFunction.c
gcc -std=c99 -Wall -I myInclude/ -c -o error.o myInclude/error.c
gcc -std=c99 -Wall -I myInclude/ -c -o unit.o myInclude/unit.c
gcc -std=c99 -Wall -I myInclude/ -c -o unitTest.o myInclude/unitTest.c
gcc -std=c99 -Wall -I myInclude/ -c -o codeList_13.1.o symbol_try/codeList_13.1/codeList_13.1.c
gcc -o symbol otherFunction.o error.o unit.o unitTest.o codeList_13.1.o
Why don't you create a library from the objects in myInclude and do the linking in the Makefile in your code path (symbol_try/codeList_13.1). The latter is better anyway because the needed libraries (-pthread -lpthread in your case) might change as well for some other code.
The main Makefile now would have got nothing to do but call make in all needed subdirectories.
In each folder have a makefile with
SOURCES=sample.c sampletest.c
OBJECTS=$(SOURCES:%.c=$(OBJDIR)/%.o)
all: $(OBJECTS)
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -o $# $<
In the root directory of a project, create a makefile with a rule to compile every sub-folder like the below.
Dirs= path-to-rootdir
objs:
set -e ; \
for i in $(Dirs) ; do \
$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS_MODULE)" LDFLAGS="$(LDFLAGS)" OBJDIR="$(OBJDIR)" -C $$i; \
done
And then you could use it build the executable by adding a rule
EXE: objs
$(CC) -L./Path1 $(LIB_PATH) -llib1 -o $(EXE_NAME) $(wildcard $(OBJDIR)/*.o)
Hope this helps!!!

how to create a makefile linux

I have the following: a .c code, a .h code plus a .o file and a -lib... library. When I compile the c code with gcc I do:
gcc code.c -o code file.o -library
How to create a makefile?
I currently have with autoconf and automake?
I currently have:
Makefile
all:
gcc code.c -o code file.o -library
clean:
rm -f code
When I compile make I get:
make: Nothing to be done for `all'.
Need some help. Thx
I'm sorry I have to give this answer, but have you tried https://www.google.com/search?q=makefile+tutorial?
Pick one, and learn how to create a Makefile. You don't need the autotools.
It took me 5 minutes to come up with this, by reading Makefile Tutorial:
CC=gcc
CFLAGS=-Wall
CLIBS=-lX11
OUT=code
OBJS=code.o file.o
$(OUT): code.o file.o
$(CC) $(CFLAGS) $(CLIBS) -o $(OUT) $(OBJS)
code.o: code.c
$(CC) $(CFLAGS) -c code.c -o code.o
file.o: file.c
$(CC) $(CFLAGS) -c file.c -o file.o
.PHONY: clean
clean:
rm -rf *.o $(OUT)
If on the computer the -library doesn t exist I want to install it automatically.
This is specific to the distribution used and you need root access on the machine. Doing it like this is not how it's done. Instead, you should create a package specific for your distribution where you specify the dependencies.
For instance, for Ubuntu (since it's the most used distribution), you'd create a .deb file: http://wiki.debian.org/IntroDebianPackaging
This, however, has nothing to do with your initial question.
See this code this may help you.
For example create a C test file "test.c" and then create makefile as shown below.
Makefile
all:
cc -o test test.c
clean:
rm -f test
Save this file as "Makefile"
Run this makefile by make command. It then creates test executable file at present directory
Run the file using ./test
You can use make clean to delete the executable file(test).
For any reference to see Makefile tutorial

Scintilla- Can't compile bait example

I'm tring to compile the bait example from the Scintilla website. I have had ZERO luck in getting it to compile. I can compile scintilla itself just fine, as well as scite but bait is a different story. When I try and compile with the supplied Makefile, I get the following error:
bait.c:1:21: fatal error: gtk/gtk.h: No such file or directory
I have GTK, and using different versions of it.I've tried It doesn't seem to matter what I do. I've scowered the Internet for help, read through man page after man page and I just can't figure out what the problem is. If I don't get that error, then I just get another one.
Here's the original makefile:
.SUFFIXES: .c .o .h .a
INCLUDEDIRS=-I../scintilla/include
CXXFLAGS= -DGTK -DSCI_LEXER -W -Wall
LEXEROBJS=$(wildcard ../scintilla/gtk/Lex*.o)
all: bait
shiz:
gcc `pkg-config --cflags gtk+-2.0` $(INCLUDEDIRS) $(CXXFLAGS) -c $< -o $#
bait: bait.o $(LEXEROBJS) ../scintilla/bin/scintilla.a
gcc `pkg-config --libs gtk+-2.0 gthread-2.0` -lstdc++ -DGTK $^ -o $#
clean:
rm -rf bait *.o
I've also tried using GtkScintilla from codebrainz. I can't even make that work. I always get either the above error, or an error saying:
fatal error: gtk/gtk.h: No such file or directory
I'd really appreciate any and all help on this. Thank you.
Edit: I'm using Linux Mint
Coming back to this... If you've already solved the issue please let me know. What it's starting to look like is this is more trouble than it's worth. :)
here's what I did:
Install Mint 13 on my virtual box
libgtk-3-dev (and supporting packages)
verify I could build with pkg-config --cflags --libs gtk+-3.0
found gtk.h (/usr/include/gtk-3.0/gtk/gtk.h) and supporting files
downloaded bait example
Here's the problems:
bait's Makefile has references to gtk+-2.0 which you need to change to 3.0
bait's Makefile requires you to build in a specific directory (note the INCLUDEDIRS needs you to be in the scintilla directory
archive scintilla.a needs to be built (it's not provided and is required for bait.o)
building the archive needs you to modify the makefile (comment out these lines:
ifdef GTK3
else
GTKVERSION=gtk+-2.0
endif
Once you fix that and build the archive there's about a million more undefined references in: ScintillaGTK.cxx:(.text+0x1374) and the like..
Anyway. I'll give it a little more time, let me know if you close this issue yourself!
i've done with this makefile
##### Makefile #####
# Make file for bait on Linux or compatible OS
# Released to the public domain 2000 by Neil Hodgson neilh#scintilla.org
# This makefile tested with GCC 3.2 and GNOME 2.0
.SUFFIXES: .c .o .h .a
ifdef GTK3
GTKVERSION=gtk+-3.0
else
GTKVERSION=gtk+-2.0
endif
INCLUDEDIRS=-I../scintilla/include
CXXFLAGS= -DGTK -DSCI_LEXER -W -Wall
LEXEROBJS=$(wildcard ../scintilla/gtk/Lex*.o)
all: bait
.c.o:
gcc `pkg-config --cflags $(GTKVERSION)` $(INCLUDEDIRS) $(CXXFLAGS) -c $< -o $#
bait: bait.o $(LEXEROBJS) ../scintilla/bin/scintilla.a
gcc -DGTK $^ -o $# -lstdc++ `pkg-config --libs $(GTKVERSION) gthread-2.0` -lm -lgmodule-2.0
clean:
rm -rf bait *.o

Resources