目前共有5篇帖子。 內容轉換:不轉換▼
 
點擊 回復
2081 4
【编译参数】fedora19下编译含有mysql操作的c程序
一派護法 十九級
1樓 發表于:2013-8-2 14:40
先看makefile
文件名为powerlog2.c

powerlog2: powerlog2.c
    gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient

参数我是通过mysql_config -cflags和mysql_config --libs这两个终端命令找到的,执行后需要的指令一大堆。

虽然本来有一大堆参数的,但我删删减减,发现只有下列三个必须参数:
-I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
一派護法 十九級
2樓 發表于:2013-8-2 14:40
编译前需要sudo yum install mysql-devel。
然后再make。

以下是我的编译过程
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2
powerlog2.c: In function ‘main’:
powerlog2.c:6:2: error: unknown type name ‘MYSQL’
  MYSQL conn;
  ^
powerlog2.c:13:11: warning: initialization makes integer from pointer without a cast [enabled by default]
  char sql="INSERT INTO `powerlog` (time) VALUES (now())";
           ^
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/lib/mysql/ -lmysqlclient
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/lib/mysql/ -lmysqlclient
powerlog2.c: In function ‘main’:
powerlog2.c:7:2: error: unknown type name ‘MYSQL’
  MYSQL conn;
  ^
powerlog2.c:16:11: warning: initialization makes integer from pointer without a cast [enabled by default]
  char sql="INSERT INTO `powerlog` (time) VALUES (now())";
           ^
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/lib/mysql/ -lmysqlclient
powerlog2.c: In function ‘main’:
powerlog2.c:7:2: error: unknown type name ‘MYSQL’
  MYSQL conn;
  ^
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ sudo yum install mysql-devel
[sudo] password for octopus:
Sorry, try again.
[sudo] password for octopus:
Loaded plugins: langpacks, refresh-packagekit
Cairo-Dock                                               | 2.9 kB     00:00    
adobe-linux-x86_64                                       |  951 B     00:00    
google-chrome                                            |  951 B     00:00    
google-chrome/primary                                      | 1.9 kB   00:00    
google-chrome                                                               3/3
Resolving Dependencies
--> Running transaction check
---> Package mariadb-devel.x86_64 1:5.5.31-4.fc19 will be installed
--> Processing Dependency: openssl-devel(x86-64) for package: 1:mariadb-devel-5.5.31-4.fc19.x86_64
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.1e-4.fc19 will be installed
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.1e-4.fc19.x86_64
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.11.3-2.fc19 will be installed
--> Processing Dependency: libverto-devel for package: krb5-devel-1.11.3-2.fc19.x86_64
--> Processing Dependency: libselinux-devel for package: krb5-devel-1.11.3-2.fc19.x86_64
--> Processing Dependency: libcom_err-devel for package: krb5-devel-1.11.3-2.fc19.x86_64
--> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.11.3-2.fc19.x86_64
--> Running transaction check
---> Package keyutils-libs-devel.x86_64 0:1.5.5-4.fc19 will be installed
---> Package libcom_err-devel.x86_64 0:1.42.7-2.fc19 will be installed
---> Package libselinux-devel.x86_64 0:2.1.13-15.fc19 will be installed
--> Processing Dependency: libsepol-devel >= 2.1.9-1 for package: libselinux-devel-2.1.13-15.fc19.x86_64
--> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.1.13-15.fc19.x86_64
---> Package libverto-devel.x86_64 0:0.2.5-2.fc19 will be installed
--> Running transaction check
---> Package libsepol-devel.x86_64 0:2.1.9-1.fc19 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch        Version                Repository    Size
================================================================================
Installing:
 mariadb-devel            x86_64      1:5.5.31-4.fc19        updates      729 k
Installing for dependencies:
 keyutils-libs-devel      x86_64      1.5.5-4.fc19           fedora        34 k
 krb5-devel               x86_64      1.11.3-2.fc19          updates      600 k
 libcom_err-devel         x86_64      1.42.7-2.fc19          fedora        29 k
 libselinux-devel         x86_64      2.1.13-15.fc19         fedora       168 k
 libsepol-devel           x86_64      2.1.9-1.fc19           fedora        70 k
 libverto-devel           x86_64      0.2.5-2.fc19           fedora        11 k
 openssl-devel            x86_64      1:1.0.1e-4.fc19        fedora       1.2 M

Transaction Summary
================================================================================
Install  1 Package (+7 Dependent packages)

Total download size: 2.8 M
Installed size: 7.3 M
Is this ok [y/d/N]: y
Downloading packages:
(1/8): keyutils-libs-devel-1.5.5-4.fc19.x86_64.rpm         |  34 kB   00:02    
(2/8): libverto-devel-0.2.5-2.fc19.x86_64.rpm              |  11 kB   00:02    
(3/8): libcom_err-devel-1.42.7-2.fc19.x86_64.rpm           |  29 kB   00:09    
(4/8): libselinux-devel-2.1.13-15.fc19.x86_64.rpm          | 168 kB   00:13    
(5/8): libsepol-devel-2.1.9-1.fc19.x86_64.rpm              |  70 kB   00:14    
(6/8): krb5-devel-1.11.3-2.fc19.x86_64.rpm                 | 600 kB   00:14    
(7/8): openssl-devel-1.0.1e-4.fc19.x86_64.rpm              | 1.2 MB   00:10    
(8/8): mariadb-devel-5.5.31-4.fc19.x86_64.rpm              | 729 kB   01:44    
--------------------------------------------------------------------------------
Total                                            26 kB/s | 2.8 MB     01:50    
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : keyutils-libs-devel-1.5.5-4.fc19.x86_64                      1/8
  Installing : libsepol-devel-2.1.9-1.fc19.x86_64                           2/8
  Installing : libselinux-devel-2.1.13-15.fc19.x86_64                       3/8
  Installing : libverto-devel-0.2.5-2.fc19.x86_64                           4/8
  Installing : libcom_err-devel-1.42.7-2.fc19.x86_64                        5/8
  Installing : krb5-devel-1.11.3-2.fc19.x86_64                              6/8
  Installing : 1:openssl-devel-1.0.1e-4.fc19.x86_64                         7/8
  Installing : 1:mariadb-devel-5.5.31-4.fc19.x86_64                         8/8
  Verifying  : 1:openssl-devel-1.0.1e-4.fc19.x86_64                         1/8
  Verifying  : libcom_err-devel-1.42.7-2.fc19.x86_64                        2/8
  Verifying  : libverto-devel-0.2.5-2.fc19.x86_64                           3/8
  Verifying  : libsepol-devel-2.1.9-1.fc19.x86_64                           4/8
  Verifying  : 1:mariadb-devel-5.5.31-4.fc19.x86_64                         5/8
  Verifying  : keyutils-libs-devel-1.5.5-4.fc19.x86_64                      6/8
  Verifying  : libselinux-devel-2.1.13-15.fc19.x86_64                       7/8
  Verifying  : krb5-devel-1.11.3-2.fc19.x86_64                              8/8

Installed:
  mariadb-devel.x86_64 1:5.5.31-4.fc19                                         

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.5-4.fc19                                    
  krb5-devel.x86_64 0:1.11.3-2.fc19                                            
  libcom_err-devel.x86_64 0:1.42.7-2.fc19                                      
  libselinux-devel.x86_64 0:2.1.13-15.fc19                                     
  libsepol-devel.x86_64 0:2.1.9-1.fc19                                         
  libverto-devel.x86_64 0:0.2.5-2.fc19                                         
  openssl-devel.x86_64 1:1.0.1e-4.fc19                                         

Complete!
[octopus@pc3 powerlog]$ sudo yum install mysql-devel
Loaded plugins: langpacks, refresh-packagekit
Package 1:mariadb-devel-5.5.31-4.fc19.x86_64 already installed and latest version
Nothing to do
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/lib/mysql/ -lmysqlclient
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/include/mysql/ -lmysqlclient
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql
powerlog2.c: In function ‘main’:
powerlog2.c:14:2: warning: passing argument 1 of ‘mysql_query’ from incompatible pointer type [enabled by default]
  mysql_query("SET NAMES utf8");
  ^
In file included from powerlog2.c:3:0:
/usr/include/mysql/mysql.h:442:14: note: expected ‘struct MYSQL *’ but argument is of type ‘char *’
 int  STDCALL mysql_query(MYSQL *mysql, const char *q);
              ^
powerlog2.c:14:2: error: too few arguments to function ‘mysql_query’
  mysql_query("SET NAMES utf8");
  ^
In file included from powerlog2.c:3:0:
/usr/include/mysql/mysql.h:442:14: note: declared here
 int  STDCALL mysql_query(MYSQL *mysql, const char *q);
              ^
powerlog2.c:17:2: warning: passing argument 1 of ‘mysql_query’ from incompatible pointer type [enabled by default]
  mysql_query(sql);
  ^
In file included from powerlog2.c:3:0:
/usr/include/mysql/mysql.h:442:14: note: expected ‘struct MYSQL *’ but argument is of type ‘char *’
 int  STDCALL mysql_query(MYSQL *mysql, const char *q);
              ^
powerlog2.c:17:2: error: too few arguments to function ‘mysql_query’
  mysql_query(sql);
  ^
In file included from powerlog2.c:3:0:
/usr/include/mysql/mysql.h:442:14: note: declared here
 int  STDCALL mysql_query(MYSQL *mysql, const char *q);
              ^
powerlog2.c:18:2: error: too few arguments to function ‘mysql_close’
  mysql_close();
  ^
In file included from powerlog2.c:3:0:
/usr/include/mysql/mysql.h:846:14: note: declared here
 void STDCALL mysql_close(MYSQL *sock);
              ^
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql
/tmp/ccmaofcA.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -lmysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql
/tmp/ccBSwNsn.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L /usr/lib/mysql/libmysqlclient.a -lz -L/usr/lib/mysql -lmysqlclient#-I/usr/include/mysql
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib/mysql -lz
/tmp/cc2Im4ua.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql
/tmp/ccL557U7.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ mak
bash: mak: command not found...
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L /usr/local/mysql/lib/*.a -lz
/tmp/ccY9nk5w.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ sudo yum install mysql-client
[sudo] password for octopus:
Sorry, try again.
[sudo] password for octopus:
Loaded plugins: langpacks, refresh-packagekit
No package mysql-client available.
Error: Nothing to do
[octopus@pc3 powerlog]$ sudo yum install libmysql++
Loaded plugins: langpacks, refresh-packagekit
No package libmysql++ available.
Error: Nothing to do
[octopus@pc3 powerlog]$ sudo yum install libmysql
Loaded plugins: langpacks, refresh-packagekit
No package libmysql available.
Error: Nothing to do
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I /usr/include/mysql -l mysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I /usr/include/mysql -l mysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ mysql_config --libs
-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I /usr/include/mysql -l mysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ mysql_config -cflags
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
        --cflags         [-I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1]
        --include        [-I/usr/include/mysql]
        --libs           [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl]
        --libs_r         [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl]
        --plugindir      [/usr/lib64/mysql/plugin]
        --socket         [/var/lib/mysql/mysql.sock]
        --port           [0]
        --version        [5.5.31]
        --libmysqld-libs [-L/usr/lib64/mysql -lmysqld]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/include/mysql]
                pkglibdir     [/usr/lib64/mysql]
                plugindir     [/usr/lib64/mysql/plugin]
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
In file included from /usr/include/stdio.h:27:0,
                 from powerlog2.c:2:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
 #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
    ^
/tmp/ccXjuFfv.o: In function `main':
/home/octopus/Programs/powerlog/powerlog2.c:8: undefined reference to `mysql_init'
/home/octopus/Programs/powerlog/powerlog2.c:9: undefined reference to `mysql_real_connect'
/home/octopus/Programs/powerlog/powerlog2.c:15: undefined reference to `mysql_query'
/home/octopus/Programs/powerlog/powerlog2.c:18: undefined reference to `mysql_query'
/home/octopus/Programs/powerlog/powerlog2.c:19: undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc -o powerlog2  powerlog2.c
powerlog2.c:3:19: fatal error: mysql.h: No such file or directory
 #include <mysql.h>
                   ^
compilation terminated.
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ mysql_config --libs
-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
[octopus@pc3 powerlog]$ mysql_config --cflags
-I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 powerlog2.c -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
In file included from /usr/include/stdio.h:27:0,
                 from powerlog2.c:2:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
 #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
    ^
[octopus@pc3 powerlog]$ ls
autorun.sh  Makefile  oldversion  powerlog2  powerlog2.c
[octopus@pc3 powerlog]$ ./powerlog2
[octopus@pc3 powerlog]$ make
make: `powerlog2' is up to date.
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
In file included from /usr/include/stdio.h:27:0,
                 from powerlog2.c:2:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
 #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
    ^
[octopus@pc3 powerlog]$ ./powerlog2
Welcome
[octopus@pc3 powerlog]$ make
make: `powerlog2' is up to date.
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
gcc: error: unrecognized command line option ‘-Wp’
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
#gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto
#gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
[octopus@pc3 powerlog]$ make
#gcc powerlog2.c -o powerlog2
#-I /usr/include/mysql -l mysqlclient
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto
#gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fPIC  -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1 -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
[octopus@pc3 powerlog]$ clear

[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lssl -lcrypto
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lcrypto
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lz -lm
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -lz
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
[octopus@pc3 powerlog]$ make
gcc powerlog2.c -o powerlog2 -I/usr/include/mysql -L/usr/lib64/mysql
/tmp/ccRsGTYv.o: In function `main':
powerlog2.c:(.text+0x16): undefined reference to `mysql_init'
powerlog2.c:(.text+0x51): undefined reference to `mysql_real_connect'
powerlog2.c:(.text+0x7e): undefined reference to `mysql_query'
powerlog2.c:(.text+0xfd): undefined reference to `mysql_query'
powerlog2.c:(.text+0x10c): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
make: *** [powerlog2] Error 1
[octopus@pc3 powerlog]$

一派護法 十九級
3樓 發表于:2013-8-2 14:43
看最后几次make,倒数第一次make因为缺少了 -lmysqlclient而失败,但倒数第二次就成功了。说明那三个命令缺一不可。

-I/usr/include/mysql主要是为了防止找不到#include <mysql.h>
-L/usr/lib64/mysql -lmysqlclient这两个参数必须配合使用,光写 -lmysqlclient是不行的,会提示找不到“ -lmysqlclient”。但光写-L/usr/lib64/mysql就是倒数第一次那样的后果。
一派護法 十九級
4樓 發表于:2013-8-2 14:46
不能把mysql_config -cflags和mysql_config --libs里面显示的所有参数都复制进去,否则会有一个warning的:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
 #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
    ^

虽然编译成功但有个warning也不好看嘛
反正以后编译只需要执行:
gcc xxx.c -o xxx -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
就行了,还不行就
sudo yum install mysql-devel
一派護法 十九級
5樓 發表于:2013-8-2 14:48
以下是我c程序的代码,数据库密码已隐藏
//Version: 2.0
#include <stdio.h>
#include <mysql.h>   //根本不需要绝对路径,不像网上说的那样

int main()
{
    MYSQL conn;
    mysql_init(&conn);
    if (!mysql_real_connect(&conn,"localhost","用户名","密码","数据库名",0,NULL,0))
    {
        printf("Can not connect the database server.\n");
        return 1;
    }
   
    mysql_query(&conn,"SET NAMES utf8");
   
    char sql[]="INSERT INTO `powerlog` (time) VALUES (now())";
    mysql_query(&conn,sql);
    mysql_close(&conn);
   
    printf("Welcome\n");
   
    return 0;
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:2081 回複數:4
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2013-8-2 14:48
精品區:常見系統問題
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。