views:

75

answers:

2

I recently got MySQL compiled and working on Cygwin, and got a simple test example from online to verify that it worked. The test example compiled and ran successfully.

However, when incorporating MySQL in a hobby project of mine it isn't compiling which I believe is due to how the Makefile is setup, I have no experience with Makefiles and after reading tutorials about them, I have a better grasp but still can't get it working correctly.

When I try and compile my hobby project I recieve errors such as:

Obj/Database.o:Database.cpp:(.text+0x492): undefined reference to `_mysql_insert_id'
Obj/Database.o:Database.cpp:(.text+0x4c1): undefined reference to `_mysql_affected_rows'
collect2: ld returned 1 exit status
make[1]: *** [build] Error 1
make: *** [all] Error 2

Here is my Makefile, it worked with compiling and building the source before I attempted to put in MySQL support into the project. The LIBMYSQL paths are correct, verified by 'mysql_config'.

COMPILER = g++
WARNING1 = -Wall -Werror -Wformat-security -Winline -Wshadow -Wpointer-arith
WARNING2 = -Wcast-align -Wcast-qual -Wredundant-decls 
LIBMYSQL = -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient
DEBUGGER = -g3
OPTIMISE = -O

C_FLAGS =  $(OPTIMISE) $(DEBUGGER) $(WARNING1) $(WARNING2) -export-dynamic $(LIBMYSQL)
L_FLAGS = -lz -lm -lpthread -lcrypt $(LIBMYSQL)

OBJ_DIR = Obj/
SRC_DIR = Source/
MUD_EXE = project
MUD_DIR = TestP/
LOG_DIR = $(MUD_DIR)Files/Logs/

ECHOCMD = echo -e
L_GREEN = \e[1;32m
L_WHITE = \e[1;37m
L_BLUE  = \e[1;34m
L_RED   = \e[1;31m
L_NRM   = \e[0;00m

DATE    = `date +%d-%m-%Y`
FILES   = $(wildcard $(SRC_DIR)*.cpp)
C_FILES = $(sort $(FILES))
O_FILES = $(patsubst $(SRC_DIR)%.cpp, $(OBJ_DIR)%.o, $(C_FILES))

all:
     @$(ECHOCMD) "  Compiling $(L_RED)$(MUD_EXE)$(L_NRM).";
     @$(MAKE) -s build

build: $(O_FILES)
     @rm -f $(MUD_EXE)
     $(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)
     @echo "  Finished Compiling $(MUD_EXE).";
     @chmod g+w $(MUD_EXE)
     @chmod a+x $(MUD_EXE)
     @chmod g+w $(O_FILES)

$(OBJ_DIR)%.o: $(SRC_DIR)%.cpp
     @echo "  Compiling $@";
     $(COMPILER) -c $(C_FLAGS) $< -o $@

.cpp.o:
     $(COMPILER) -c $(C_FLAGS) $<

clean:
     @echo "  Complete compile on $(MUD_EXE).";
     @rm -f $(OBJ_DIR)*.o $(MUD_EXE)
     @$(MAKE) -s build

I like the functionality of the Makefile, instead of spitting out all the arguments etc, it just spits out the "Compiling [Filename]" etc.

If I add -c to the L_FLAGS then it compiles (I think) but instead spits out stuff like:

g++: Obj/Database.o: linker input file unused because linking not done

After a full day of trying and research on google, I'm no closer to solving my problem, so I come to you guys to see if you can explain to me why all this is happening and if possible, steps to solve.

Regards, Steve

A: 

Have you tried using the built-in mysql_config --cflags and mysql_config --libs, respectively?

Chris
I tried placing the mysql_config --libs one within the makefile, didn't try the others, however it gave the same result as the above, i've verified all of the paths aswell. With the test program that I mentioned, it compiled and built successfully with: $ make g++ -Wall -Wextra -c -g -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient cygwintest.cpp g++ cygwintest.o -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient -lz -o cygwintest.exe
Steve
Sorry for the formatting..
Steve
+1  A: 

Try changing

$(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)

to

$(COMPILER) -o $(MUD_EXE) $(O_FILES) $(L_FLAGS)

The linker searches and processes libraries and object files in the order they are specified. Thus when you mention the libraries before the object files, the functions used in the object files may not be loaded from the libraries.

Dmitry Yudakov
That worked! Thank you!! It now successfully compiles and builds, thank you again!
Steve