recently I've been learning how to program GTK+ applications and MySQL in C programming language.
I wanted to try writing a program with both of those libs at once, I've done it but I can't compile it...
When I compile a program which uses MySQL I do this:
gcc exa_7.c -o exa_7 -std=c99 `mysql_config --cflags --libs`
and when I compile GTK+ application I do this:
gcc -o test_5 test_5.c `pkg-config --libs --cflags gtk+-2.0`
I can't write both of them, I mean pkg-config and mysql_config at once, because compiler won't accept it. I've been trying to find some makefile scripts but haven't found anything usefull. Please help. Thanks.
You're supposed to do this:
gcc exa_7.c -o exa_7 -std=c99 `mysql_config --cflags --libs` `pkg-config --libs --cflags gtk+-2.0`
If the backticks are causing you problems, you can run the programs using $() instead:
gcc exa_7.c -o exa_7 -std=c99 $(mysql_config --cflags --libs) $(pkg-config --libs --cflags gtk+-2.0)
Related
I'm trying to connect my app to a MySQL database on Linux.
I compile my app using this command:
g++ -o mysqlconnect $(mysql_config --cflags) db.cpp $(mysql_config --libs)
and it works fine. But I can't seem to find a way to add $(mysql_config --cflags) and $(mysql_config --libs) to my make file. Is there any way?
If we are talking GNU Makefiles, the synthax is
g++ -o mysqlconnect $(shell mysql_config --cflags) db.cpp $(shell mysql_config --libs)
See the GNU make documentation.
I'm writing a program to monitor a few network switches. Today I tried added the libssh library to the project, but can't seem to get it to compile. It seems like a simple thing, but I couldn't find the answer googling.
I was able to compile a test program with the command.
gcc libssh.c -lssh
I can't seem to get the -lssh to work in my makefile.
CC=gcc
CFLAGS=-I.
LIBS=`net-snmp-config --libs` `mysql_config --cflags --libs` `-lssh`
TARGET = snmpmon
$(TARGET): $(TARGET).c
$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c $(LIBS)
clean:
$(RM) $(TARGET)
I guessing it's something simple any suggestion?
gcc -I. -o snmpmon snmpmon.c `net-snmp-config --libs` `mysql_config --cflags --libs` `-lssh --libs`
/bin/sh: 1: -lssh: not found
/tmp/cc65Loeb.o: In function `GetMACinfo':
snmpmon.c:(.text+0x26): undefined reference to `ssh_new'
snmpmon.c:(.text+0x47): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x5c): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x71): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x7d): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'snmpmon' failed
make: *** [snmpmon] Error 1
OK the issue was -lssh should be -lssh the makefile didn't like the extra ` marks.
I am trying to develop an application in C in a target Linux system which requires Mysql Conectivity but i dont know where to include in NETBEANS the required directives for the libmysqlclient-dev library.
I have the following:
A laptop with NETBEANS IDE 8.0.2 and remote build host setup.
A remote Ubuntu linux target which is the remote build host for netbeans.
apt-get install libmysqlclient-dev in the Ubuntu Target
mysql_config --libs gives:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
mysql_config --libs gives:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
According to the Mysql C Api Building I have to include the following:
gcc -c `mysql_config --cflags` progname.c
gcc -o progname progname.o `mysql_config --libs`
Although I am able to build my program manually in the target system, I am not sure where to add the above information in Netbeans.
P.S.1 at the momment my Netbeans build command looks like this:
gcc -o dist/Debug/GNU-Linux-x86/arguments_1 build/Debug/GNU-Linux-x86/src/args.o
P.S.2 Please be gentle. I am a newbie with Netbeans, Remote builds, C and Linux development.
Ok I have managed to get it working.
First I needed to include the Mysql Library paths to the Netbeans makefile as per this post:
gcc wont compile and run MySQL C libraries
# These are the flags that gcc requires in order to link correctly against our installed
# client packages
MYSQL_LIBS := $(shell mysql_config --cflags --libs)
Then right click on my project node , select Properties->Build->Linker->Compilation Line->Additional Options and add $(MYSQL_LIBS) to the Additional options parameter.
My problem was that I was adding it into the C compiler Additional options parameter.
But this post helped to clarify the order:
Why does the order in which libraries are linked sometimes cause errors in GCC?
So now my Netbeans gcc command looks like:
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/src/args.o.d" -o build/Debug/GNU-Linux-x86/src/args.o src/args.c
gcc -o dist/Debug/GNU-Linux-x86/arguments_1 build/Debug/GNU-Linux-x86/src/args.o -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
Thank you Lumi, Thanassis
I have trouble installing the mysql udf (https://github.com/mysqludf/lib_mysqludf_sys). Here's what I'm getting:
Compiling the MySQL UDF
gcc -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o /usr/lib/lib_mysqludf_sys.so
/usr/bin/ld: /tmp/ccw6HRtN.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccw6HRtN.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [install] Error 1
ERROR: You need libmysqlclient development software installed
to be able to compile this UDF, on Debian/Ubuntu just run:
apt-get install libmysqlclient15-dev
Any ideas? TIA
UPDATED:
I have already the libmysqlclient15-dev installed.
Try to recompile with -fPIC flag. It obviously hints at it:
/usr/bin/ld: /tmp/ccw6HRtN.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
I know this is an old question, however I've just had this issue using Ubuntu 16.
I solved changing the Makefile in the following way, as explained in a GitHub issue:
LIBDIR=/usr/lib/mysql/plugin
install:
gcc -DMYSQL_DYNAMIC_PLUGIN -fPIC -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o $(LIBDIR)/lib_mysqludf_sys.so
This might sound like a dumb question. But here goes..... I am using a C program called db_access.c which interacts with MySQL (in Ubuntu 10.10 with MySQL Server version: 5.1.49-1ubuntu8.1 (Ubuntu)). Inside the program, I have: include "mysql.h"
When I do the following, everything works out right:
gcc -I/usr/include/mysql db_access.c -lmysqlclient -o db_access
./db_access
Problem arises when I try to integrate it into an existing (and working makefile). The contents of the makefile:
all: MappingServer
#Macro definitions
CC = gcc
CFLAGS = -lm
INCLUDES = -I/usr/include/mysql
LIBS = -L/usr/lib/mysql -lmysqlclient
MappingServer.o: MappingServer.c map_registration.h
$(CC) $(CFLAGS) -c MappingServer.c
route_aggregation.o: route_aggregation.c map_registration.h
$(CC) $(CFLAGS) -c route_aggregation.c
db_access.o: db_access.c map_registration.h mysql.h
$(CC) $(CFLAGS) $(INCLUDES) -c db_access.c
MappingServer: MappingServer.o route_aggregation.o db_access.o
$(CC) $(LIBS) -o MappingServer MappingServer.o route_aggregation.o db_access.o
clean:
-rm MappingServer.o route_aggregation.o db_access.o
I have two other C programs, MappingServer.c and route_aggregation.c. These 3 files need to be compiled together. By the way, I also did:
root#ahuq-kitchen:/home/ahuq/MappingServer# mysql_config --cflags
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
and
root#ahuq-kitchen:/home/ahuq/MappingServer# mysql_config --libs
-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient
So I think the paths are OK. When I do: make all
I get:
root#ahuq-kitchen:/home/ahuq/MappingServer# make all
gcc -lm -c MappingServer.c
gcc -lm -c route_aggregation.c
route_aggregation.c: In function ‘vtysh_input’:
route_aggregation.c:602: warning: function returns address of local variable
make: *** No rule to make target `mysql.h', needed by `db_access.o'. Stop.
Why is this happening?
the line
db_access.o: db_access.c map_registration.h mysql.h
tells make that db_access.o depends on db_access.c, map_registration.h and mysql.h. make complains because mysql.h cannot be found in the current directory (it's in /usr/include/mysql).
see the question Makefile updated library dependency for how to specify libraries as dependencies in make
You put "mysql.h" as a dependency, but it's not in the current directory, so Make thinks it needs to build it, but doesn't know how.
try to remove all the lines like:
MappingServer.o: MappingServer.c map_registration.h
if the map_registration.h is included in the c file, make is smart enough to find it. The only thing to be noticed may be to set the search file path using: -I.