在 CMake 中链接 MySQL 库

Posted

技术标签:

【中文标题】在 CMake 中链接 MySQL 库【英文标题】:Link MySQL Librarys in CMake 【发布时间】:2011-10-30 13:26:35 【问题描述】:

我开始学习如何使用 CMake,但我在链接库方面遇到了一些问题。现在我的问题是 mysql (C)。由于默认情况下我包含了一个我找到的 FindMySQL.cmake,但是它并没有解决,因为我的库位于项目中的单独文件夹中。

我的项目结构:

/

/CMakeLists.txt

/包括

/include/mysql (...)

/lib (libmysql.lib, mysqlclient.lib, libmysql.dll)

/src (main.cpp)

/src/login (login.cpp, login.h)

/build(CMake的构建目录)

很抱歉没有组织,但是我有这个问题已经很久了,我用的是“愤怒”。

我当前的 CMakeLists:

# eGest - Product Sales by Console
# Copyright (C) 2011 Bruno Alano
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
# CMake - Build System

# Minium CMake Version
cmake_minimum_required (VERSION 2.6)

# Project Name
project (egest)

# Add MySQL
# find_package(MySQL REQUIRED)
include_directories(include)
set(CMAKE_LIBRARY_PATH /lib)
set(LIBS $LIBS mysql)

# Include Directory
include_directories(src)

# Link the MySQL
# Add Sources
add_executable(test src/egest.cpp src/login/login.cpp)
target_link_libraries(test $LIBS)

但是如果我使用它,返回这个错误:

C:\Users\ALANO\eGest\build>cmake .. -G "MinGW Makefiles"
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ALANO/eGest/build

C:\Users\ALANO\eGest\build>make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/src/egest.cpp.obj
[100%] Building CXX object CMakeFiles/test.dir/src/login/login.cpp.obj
Linking CXX executable test.exe
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot fin
d -lmysql
collect2: ld returned 1 exit status
make[2]: *** [test.exe] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

谢谢,布鲁诺·阿拉诺。

【问题讨论】:

【参考方案1】:

CMAKE_LIBRARY_PATH 是 CMake 放置它构建的库的位置,而不是它查找现有库的位置。

尝试在add_executable 之前添加link_directories("$PROJECT_SOURCE_DIR/lib")

然后执行make VERBOSE=1 以查看传递了哪些编译器选项——希望其中之一是您刚刚添加的-L(库搜索路径)。

【讨论】:

抱歉,这不起作用。 VERBOSE=1 的输出。 heypasteit.com/clip/01WZ 我用更好(和更多 CMakey)的方式更新了我的答案。请尝试一下,看看是否有帮助。

以上是关于在 CMake 中链接 MySQL 库的主要内容,如果未能解决你的问题,请参考以下文章

如何在 cmake 中正确链接库?

为啥cmake没有在makefile中添加库链接命令?

如何在 CMake 中链接预编译库?

cmake设置默认静态链接库

使用 c++/cmake 链接静态库

CMake - 更改共享库链接选项