当前位置:Gxlcms > 数据库问题 > linux环境下写C++操作mysql(二)

linux环境下写C++操作mysql(二)

时间:2021-07-01 10:21:17 帮助过:10人阅读

<stdlib.h> #include"mysqlInterface.h" int main() { CMysqlInterface mysqlObj; mysqlObj.connect(); return 0; }

mysqlInterface.h

class CMysqlInterface
{
    public:
        CMysqlInterface();
        ~CMysqlInterface();
        int connect();
    private:
        MYSQL* mysqlPtr;
};

mysqlInterface.cpp

#include"mysqlInterface.h"

CMysqlInterface::CMysqlInterface()
{
    printf("CMysqlInterface\n");
    mysqlPtr = NULL;
    mysqlPtr = mysql_init(NULL);
    if(!mysqlPtr)
    {
        printf("mysql_init failed\n");
    }
}

CMysqlInterface::~CMysqlInterface()
{
    mysql_close(mysqlPtr);
    mysqlPtr = NULL;
}

int CMysqlInterface::connect()
{
    int iRet = -1;
    mysql_real_connect(mysqlPtr,"localhost","root","csql","child",0,NULL,0);
    if(!mysqlPtr)
    {
        printf("connect failed\n");
    }
    else
    {
        printf("connect success\n");
        iRet = 0;
    }
    return iRet;
}

Makefile:

Makefile文件
test : main.o mysqlInterface.o
    g++ main.o mysqlInterface.o -o test -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

main.o:main.cpp
    g++ -c main.cpp  -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient
mysqlInterface.o : mysqlInterface.cpp 
    g++ -c mysqlInterface.cpp  -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

.PHONY:clean
clean:
    rm -f main.o mysqlInterface.o  test

执行结果:

make

g++ -c main.cpp  -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

g++ -c mysqlInterface.cpp  -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

g++ main.o mysqlInterface.o -o test -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

 

./test

CMysqlInterface

connect success

程序运行成功

 

Makefile升级版:

src = $(wildcard ./*cpp)
obj = $(patsubst %.cpp , %.o , $(src))
target = test
CC = g++

$(target) : $(obj)
    $(CC) $(obj) -o $(target) -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

%.o: %.cpp
    $(CC) -c $< -o $@ -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

.PHONY:clean
clean:
    rm -f $(obj)$(target)


文件解释:

src = $(wildcard ./*cpp)

扩展通配符:搜索指定文件。

这句话代表的意思是再当前目录下搜索所有的 .cpp文件并赋值给变量 src。函数执行结束后,src的值就是 main.cp 和 mysqlInterface.cpp 

obj = $(patsubst %.cpp , %.o , $(src))

patsubst,替换通配符,按照指定规则做替换。

此行代码的意思的是将src里的每个.cpp文件 都替换成 .o文件赋值给 obj变量。

这句话还可以替换成obj = $(src: %.c = %.o) //也是将src里的每个文件都是.c替换.o

 

Makefile再升级:

src = $(wildcard ./*cpp)
obj = $(patsubst %.cpp , %.o , $(src))

target = test
CC = g++

$(target) : $(obj)
        $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS)  $(obj) -o $(target)  -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient 

%o: %.cpp
    $(CC) $(CFLAGS) -c $< -o $@ -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient

.PHONY:clean
clean:
    rm -f $(target)  $(obj)

遇到的bug:

obj = $(patsubst %.cpp , %.o , $(src))

%.cpp,%.o不能有空格

如果有空格,那obj的值还是.cpp文件,不会转化为.o文件

 

%o: %.cpp

如果%.o少了,会报mysql.h找不到的错误
In file included from main.cpp:3:0:

mysqlInterface.h:2:18: fatal error: mysql.h: 没有那个文件或目录

 #include"mysql.h"

                  ^

compilation terminated.

 

成功版本:

CFLAGS := -g
target := test
INCLUDE =  -I /usr/include/mysql
lib = -lmysqlclient 
dirlib = -L /usr/lib/mysql/ 

src = $(wildcard *.cpp)
objs = $(patsubst %.cpp,%.o,$(src))

all: $(target)

%.o: %.cpp
        #g++ $(CFLAGS)   $(INCLUDE) $(lib) $(dirlib) -c $< -o $@ 
        g++ $(CFLAGS)   $(INCLUDE) $(lib) $(dirlib) -c $< -o $@ 

$(target) : $(objs)
        g++ $(CFLAGS)  $(objs) $(dirlib) $(lib) -o $(target)  $(INCLUDE)

.PHONY:clean
clean:
    rm -f $(target)  $(obj)

 

如果make的文件名是Makefile或makefile,直接执行make

如果是别的,比如Makefile1,就可以执行make -f Makefile1

 

执行结果:

exbot@ubuntu:~/wangqinghe/MySql/20190621/01/01$ make -f Makefile1

#g++ -g   -I /usr/include/mysql -lmysqlclient  -L /usr/lib/mysql/  -c main.cpp -o main.o

g++ -g   -I /usr/include/mysql -lmysqlclient  -L /usr/lib/mysql/  -c main.cpp -o main.o

#g++ -g   -I /usr/include/mysql -lmysqlclient  -L /usr/lib/mysql/  -c mysqlInterface.cpp -o mysqlInterface.o

g++ -g   -I /usr/include/mysql -lmysqlclient  -L /usr/lib/mysql/  -c mysqlInterface.cpp -o mysqlInterface.o

g++ -g  main.o mysqlInterface.o -L /usr/lib/mysql/  -lmysqlclient  -o test  -I /usr/include/mysql

 

程序运行结果;

exbot@ubuntu:~/wangqinghe/MySql/20190621/01/01$ ./test
CMysqlInterface
connect success

linux环境下写C++操作mysql(二)

标签:lib   目录   ini   led   cal   遇到   test   linux   空格   

人气教程排行