Linux-Cpp-D01-使用靜態庫

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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章