我开始学习如何使用 CMake,但我在链接库方面遇到了一些问题。现在我的问题是 MySQL (C)。由于默认情况下有 FindMySQL.cmake,我包含了一个我发现的,但是它没有解决,因为我的库位于项目中的单独文件夹中。
我的项目结构:
/
/ CMakeLists.txt
/include
/include/mysql (...)
/lib (libmysql.lib, mysqlclient.lib, libmysql.dll)
/src (main.cpp)
/src/login (login.cpp, login.h)
/build (Build Directory of 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
谢谢,布鲁诺·阿拉诺。
最佳答案
CMAKE_LIBRARY_PATH
是 CMake 放置它构建的库的地方,而不是它查找现有库的地方。
尝试在 add_executable
之前添加 link_directories("${PROJECT_SOURCE_DIR}/lib")
。
然后执行 make VERBOSE=1
以查看传递了哪些编译器选项——希望其中之一是您刚刚添加的 -L
(库搜索路径) .
关于c++ - 在 CMake 中链接 MySQL 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7945166/