在linux环境c++编程下导入第三方库
实例
需要导入mysql第三方库
头文件位置:/usr/include/mysql
静态库文件位置:/usr/lib/mysql 下的
#include <mysql.h>
#include <iostream>
using namespace std;
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
const char *server = "localhost";
const char *user = "root";
const char *password = "123"; /* 此处改成你的密码 */
const char *database = "mysql"; /* 数据库名字,比如mysql*/
conn = mysql_init(NULL);
/* 连接数据库 */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
cout << mysql_error(conn);
return -1;
}
/* 查询语句 */
if (mysql_query(conn, "show tables")) {
cout << mysql_error(conn);
return -2;
}
res = mysql_use_result(conn);
/* 打印查询结果 */
while ((row = mysql_fetch_row(res)) != NULL) printf("%s \n", row[0]);
/* 断开连接 */
mysql_free_result(res);
mysql_close(conn);
}
一、直接通过命令行编译生成
prog:main.o
g++ -o prog main.o -L /usr/lib/mysql -lmysqlclient
main.o:main.cpp
g++ -c main.cpp -I /usr/include/mysql
clean:
rm -rf *.o prog
tips:-I表示头文件目录
-L表示库文件目录 + 库文件
当包含多个头文件or库文件时,-I path1 -I path2 …
二、vscode编译(主要还是通过makefile进行编译)
第一步:vscode打开项目目录
第二步:建立c_cpp_properties.json配置文件
mkdir .vscode
touch c_cpp_properties.json
第三步:引入第三方库头文件
(在编译器中能找到第三方头文件,以防你在敲代码的时候报错,就是好看)
在c_cpp_properties.json中复制下面,
includePath替换你第三方库的头文件位置
{
“configurations”: [
{
“name”: “Linux”,
“includePath”: [
“${workspaceFolder}/**”,
“/usr/include/mysql”
],
“defines”: [],
“compilerPath”: “/usr/bin/g++”,
“cStandard”: “c17”,
“cppStandard”: “gnu++17”,
“intelliSenseMode”: “linux-gcc-x64”
}
],
“version”: 4
}
第四步:建立makefile模板
# 根目录的Makefile
# 定义编译器和编译选项
CXX := g++
CXXFLAGS := -std=c++11 -Wall -w
# 定义第三方库的头文件和库文件路径
THIRD_PARTY_INCLUDE := -I protobuf/include/
THIRD_PARTY_LIB := -L protobuf/lib64/ -lprotobuf
THIRD_PARTY_INCLUDE2 := -I /usr/include/mysql
THIRD_PARTY_LIB2 := -L /usr/lib/mysql -lmysqlclient
# 列出所有的源文件
SRCS_MAIN := main.cpp
SRCS_DOMAIN := domain/BaseDomain.cpp domain/FlightPlight.cpp domain/MainTable.cpp domain/LatLongTable.cpp
SRCS_SERVER := Server/HSocket.cpp Server/Messages.cpp Server/MyServer.cpp Server/Util.cpp
SRCS_DAO := Dao/BaseDao.cpp Dao/FlightPlanDao.cpp Dao/MainTableDao.cpp Dao/LatLongTableDao.cpp
# 根据源文件生成目标文件的名字(替换.cpp为.o)
OBJS_MAIN := $(SRCS_MAIN:.cpp=.o)
OBJS_DOMAIN := $(SRCS_DOMAIN:.cpp=.o)
OBJS_SERVER := $(SRCS_SERVER:.cpp=.o)
OBJS_DAO = $(SRCS_DAO:.cpp=.o)
# 目标可执行文件名
TARGET := prog
# 编译规则
all: $(TARGET)
$(TARGET): $(OBJS_MAIN) $(OBJS_DOMAIN) $(OBJS_SERVER) $(OBJS_DAO) #链接
$(CXX) $(CXXFLAGS) -o $@ $^ $(THIRD_PARTY_LIB) $(THIRD_PARTY_LIB2)
%.o: %.cpp #编译
$(CXX) $(CXXFLAGS) $(THIRD_PARTY_INCLUDE) $(THIRD_PARTY_INCLUDE2) -o $@ -g -c $<
# 清理生成的文件
clean:
rm -f $(OBJS_MAIN) $(OBJS_DOMAIN) $(OBJS_SERVER) $(OBJS_DAO) $(TARGET)
tips: #编译阶段加上-g , 否则在vscode里面无法调试
第五步:建立tasks.json配置文件
cd .vscode
touch tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "shell", // 标签->lanuch.json里面启动这个任务时的标识
"type": "shell", // 描述任务的类型
"command": "make", // 这个任务相当于在终端输入make
"args": [
]
}
]
}
第六步:建立lanuch.json配置文件
cd .vscode
touch lanuch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/prog", // 可执行文件的名称 在makefile里
"args": [], //
"stopAtEntry": false, //
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",// 调试器的位置
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "shell" // 找tasks.json里标签为shell的,先执行他
}
]
} //意思就是执行${workspaceFolder}/prog,在这之前先执行tasks.json里标签为shell的