1

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

./
    ./src
    ./cmake
    CMakeLists.txt

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

运行 CMake 时,一切正常。由于 MySQL 和 CURL 没有被有效链接,我得到了一堆链接器错误。我究竟做错了什么 ?

根 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)
4

1 回答 1

1

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

相反,使用

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

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

于 2012-10-29T15:04:47.070 回答