2011-10-30 44 views
3

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

我的项目结构:“生气”

/

/ 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 

谢谢,布鲁诺购买alano。

回答

4

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

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

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

+0

对不起,但doesen't工作。 VERBOSE = 1的输出。 http://www.heypasteit.com/clip/01WZ –

+1

我用更好的(和更多CMakey)方式更新了我的答案。请尝试一下,看看它是否有帮助。 –

+0

工作!谢谢^^ –