mysql下载地址
新建一个简单的main.cpp文件
#include <cstdio>
#include "mysql.h"
int main() {
MYSQL mysqlConnect;
mysql_init(&mysqlConnect);
if (!mysql_real_connect(&mysqlConnect, "127.0.0.1", "user", "password", NULL, MYSQL_PORT, NULL, 0)) {
printf("failed\n");
return -1;
}
mysql_select_db(&mysqlConnect, "testdb");
mysql_query(&mysqlConnect, "SELECT * FROM user");
printf("end\n");
return 0;
}
新建 CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(mysqltest VERSION 0.1.0)
find_path(MYSQL_ROOT_DIR mysql)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(mysql REQUIRED_VARS MYSQL_ROOT_DIR)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${MYSQL_ROOT_DIR}/include)
target_link_directories(${PROJECT_NAME} PRIVATE ${MYSQL_ROOT_DIR}/lib)
target_link_libraries(${PROJECT_NAME} libmysql)
然后编译,不出意外应该会提示找不到 MYSQL_ROOT_DIR ,此时设置为安装目录(有include , lib , bin 的那个目录)就可以了
|