Linux-Cpp-D01-使用静态库
2023-03-01 本文已影响0人
国服最坑开发
0x01 Cmake中使用.a库
使用find_library
指定静态库变量,在target_link_libraries
中使用变量。
cmake_minimum_required(VERSION 3.19)
project(hello_cpp)
set(CMAKE_CXX_STANDARD 11)
add_executable(hello_cpp main.cpp)
include_directories(
/usr/local/include/oatpp-1.3.0/oatpp
)
find_library(OATPP oatpp /usr/local/lib/oatpp-1.3.0)
target_link_libraries(
hello_cpp
${OATPP}
)
0x02 Cmake 中使用 .dylib 动态库
对比了一下, 和上面使用.a 库写法完全一样
cmake_minimum_required(VERSION 3.19)
project(tool)
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_BUILD_TYPE "Release")
add_executable(tool main.cpp clipp.h)
include_directories(
/usr/local/Cellar/mysql-connector-c++/8.0.32/include
)
find_library(mysql libmysqlcppconn8.dylib /usr/local/Cellar/mysql-connector-c++/8.0.32/lib)
target_link_libraries(
tool ${mysql}
)
附:mysql 操作例:和JDBC的写法很神似
注意
: mysqlconnect8 使用的端口一定要用33060,原因不详。
#include "mysqlx/xdevapi.h"
Session session("127.0.0.1", 33060, "root", "123456");
Schema schema = session.getSchema("db");
Table table = schema.getTable("tbl");
RowResult result = table.select("name", "age", "tel")
.where("id = :id")
.bind("id", 1734)
.execute();
for (const auto &row : result.fetchAll()) {
std::cout << row[0] << "\t" << row[1] << "\t" << row[2] << std::endl;
}
session.close();