0

我一直在尝试创建一个小型测试应用程序来开始使用 CMake 和链接库。以下完成: - 我在 Ubuntu 上安装了 libmysqlcppconn5 库(即它现在位于 /usr/lib/libmysqlcppconn5.so,只是默认安装) - 我创建了一个包含以下内容的小 .cpp 文件

#include <stdlib.h>
#include <iostream>
#include <mysql/mysql.h>

int main(int argc, char **argv) {
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
    std::cout << "mysql init failed\n";
    exit(1);
}
conn_ptr = mysql_real_connect (conn_ptr, "localhost", "root", "pw", "db", 0, NULL, 0);
if (conn_ptr) {
    std::cout << "connection success\n";
} else {
    std::cout << "connection failed\n";
}
mysql_close(conn_ptr);

我应该如何构建我的 CMakeLists.txt 以便它使用这个共享库?

4

1 回答 1

0

怎么样:

# Find and make sure the system have the header file
find_path(MYSQL_HEADER mysql/mysql.h)
if(MYSQL_HEADER STREQUAL "MYSQL_HEADER-NOTFOUND")
    message(FATAL_ERROR "Could not find the mysql/mysql.h header file")
endif()

# Find and make sure the system have the library
find_library(MYSQL_LIBMYSQLCPPCONN5 mysqlcppconn5)
if(MYSQL_LIBMYSQLCPPCONN5 STREQUAL "MYSQL_LIBMYSQLCPPCONN5-NOTFOUND")
    message(FATAL_ERROR "Could not find the mysqlcppconn5 library")
endif()

# Link the library to the target
target_link_libraries(your_target ${MYSQL_LIBMYSQLCPPCONN5})
于 2013-05-12T04:44:34.513 回答