在CMake中链接MySQL库

时间:2011-10-30 13:26:35

标签: c++ mysql makefile cmake

我开始学习如何使用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

谢谢,Bruno Alano。

1 个答案:

答案 0 :(得分:4)

CMAKE_LIBRARY_PATH是CMake建立库的地方,而不是它查找现有库的地方。

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

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