Cmake链接到mysql&卷曲,LNK2019

时间:2012-10-29 11:15:51

标签: mysql curl linker cmake

好的,我刚刚开始玩CMake。我正在尝试构建一个依赖于MySQL和CURL的项目。这是目录结构:

./
    ./src
    ./cmake
    CMakeLists.txt

就是这么简单,src只包含9个Cpp文件和一个CMakeLists.txt。

运行CMake时,一切正常。我只是因为MySQL和CURL没有被有效链接而得到一堆链接器错误。我做错了什么?

Root CMakeLists.txt:

PROJECT(IRCBot)

INCLUDE(CPack)

SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
INCLUDE(${CMAKE_SOURCE_DIR}/cmake/FindCURL.cmake)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)

FIND_PACKAGE(CURL)
IF(CURL_FOUND)
  MESSAGE(STATUS "Curl libraries found at: ${CURL_LIBRARIES}")
  MESSAGE(STATUS "Curl includes found at: ${CURL_INCLUDE_DIRS}")
else()
  MESSAGE(SEND_ERROR "Could not find cURL on your system")
ENDIF(CURL_FOUND)

FIND_PACKAGE(MySQL)
IF(MYSQL_FOUND)
    MESSAGE(STATUS "MySQL found at: ${MYSQL_INCLUDE_DIR}, ${MYSQL_LIBRARIES}")
ELSE(MYSQL_FOUND)
    MESSAGE(SEND_ERROR "Couldn't find MySQL include files and/or library")
ENDIF(MYSQL_FOUND)

ADD_SUBDIRECTORY(src)

和./src/CMakeLists.txt

FILE(GLOB_RECURSE IRCBot_files *.cpp *.h)

SET(IRCBot_src
    ${IRCBot_files}
    )

INCLUDE_DIRECTORIES(
    ${CURL_INCLUDE_DIRS}
    ${MYSQL_INCLUDE_DIR}
    )

LINK_DIRECTORIES(
    ${MYSQL_LIBRARIES}
    ${CURL_LIBRARIES}
)
ADD_EXECUTABLE(IRCBot ${IRCBot_src})

INSTALL(TARGETS IRCBot DESTINATION ${INSTALL_DIR}/bin)

1 个答案:

答案 0 :(得分:1)

在这种情况下你不需要LINK_DIRECTORIES()调用,因为它告诉编译器在哪里找到lib。

相反,请使用

TARGET_LINK_LIBRARIES(IRCBot
    ${MYSQL_LIBRARIES}
    ${CURL_LIBRARIES})

因为${MYSQL_LIBRARIES}包含绝对路径。